How to Automatically Delete Old S3 Files Using Lifecycle Rules

Amazon Web Services block storage is still one of my go to backup solutions. My backups tend to be pretty small, so the cost is negligible. Over time though, the pennies start to add up. Once the cost gets to a couple of bucks, I go in and purge backups and the cycle begins anew.

That was the process, until I learned that you can configure lifecycle rules on your S3 buckets. These rules let you do things like moving objects between storage classes, and expire and/or delete objects based on their age.

Adding an S3 Lifecycle Rule

To begin, go to the S3 bucket you’d like to add a lifecycle rule to, and click on the “Management” section. From there, click on of the “Create lifecycle rule” buttons to get things started.

Configuring an S3 Lifecycle Rule

This is where things get a little bit tricky, and Amazon uses the terms “expire” and “delete”, but you can’t necessarily configure a rule that performs both the expiration and deletion lifecycle rule actions.

In AWS-speak, expiring an object will flag it as being eligible for deletion. Deletion of expired objects happens behind the scenes, so there’s no need to configure a deletion action.

You can enter the number of days old you like to expire objects, and can even configure more nuanced rules and filtering of objects as well.

Once configure, you can sit back and let the lifecycle rule take care of things automatically.


Move current versions of objects between storage classes

Move noncurrent versions of objects between storage classes

Expire current versions of objects

Permanently delete noncurrent versions of objects

Delete expired object delete markers or incomplete multipart uploads These actions are not supported when filtering by object tags or object size.

Josh Sherman - The Man, The Myth, The Avatar

About Josh

Husband. Father. Pug dad. Musician. Founder of Holiday API, Head of Engineering and Emoji Specialist at Mailshake, and author of the best damn Lorem Ipsum Library for PHP.


If you found this article helpful, please consider buying me a coffee.