in Servers / Serverless #AWS

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.

S3 Lifecycle Rule Create Button

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.

S3 Lifecycle Rule Configuration

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