S3 is cheap per GB, but it does not delete anything on its own. The nightly backups from three years ago, the previous versions of every file, the uploads that were cut off halfway: all of it is still there, taking up space and being charged. A lifecycle rule is an automatic rule you tell the bucket once: "anything older than 90 days, delete it" or "anything untouched for 30 days, move it to cheap storage". From then on, S3 does it every day without anyone having to remember.
Before you start: what each thing means
- Bucket and object
- The bucket is the root folder in S3; each stored file is an object.
- Lifecycle rule
- An automatic rule for the bucket: after a certain time, delete or move the objects that meet a condition.
- Prefix
- The beginning of the name of the objects the rule affects, for example "copias/". That way a rule can affect one folder and not another.
- Current version and previous versions
- With versioning on, every change or deletion keeps the previous version. Previous versions take up space and are the first candidates for clean-up.
- Delete marker
- What is left when you delete a file in a versioned bucket: the file seems to have disappeared but its versions are still there.
- Incomplete multipart upload
- A large upload that was cut off halfway. Its parts keep taking up invisible space until something cleans them up.
- Storage classes
- S3 has several "drawers" with different prices: standard, infrequent access (cheaper to store, with a cost per read) and the archive ones (very cheap, but slow to return the file).
Step by step
Decide what you want to happen and when
Three questions: how long do you need to keep the backups? (90 days is usually reasonable; a year if there is a legal obligation). How many previous versions of a file do you want to be able to recover? (30 days of previous versions gives plenty of margin for an accidental deletion). Are there files that are hardly ever read but have to be kept? (candidates for cheap storage). Write down the answers: they are the numbers for the rule.
Open the bucket's rules
S3 console, go into the bucket, Management tab, Create lifecycle rule. Give it a name that says what it does (
limpiar-copias-90-dias).Choose what it applies to
If you want it to apply to the whole bucket, tick Apply to all objects in the bucket. If only to one folder, choose Limit the scope of this rule using one or more filters and type the prefix (
copias/, with the trailing slash). You can create several rules with different prefixes: one for backups, another for logs, and leave untouched the folders that must be kept forever.Tick the actions
For a backup folder, the usual thing is to tick:
- Expire current versions of objects: 90 days after creation. This deletes old backups.
- Permanently delete noncurrent versions of objects: 30 days after becoming noncurrent. This cleans up previous versions and deleted items.
- Delete expired object delete markers and delete incomplete multipart uploads after 7 days. This cleans up the invisible rubbish and should be on every bucket.
If instead of deleting you want to cut costs, tick Transition current versions of objects between storage classes: to Standard-IA after 30 days and, if it is long-term archive, to an archive class after 90. Careful: archive classes take hours to return the file and charge for retrieving it.
Review and save
The final screen summarises in a timeline what happens to each object at 7, 30 and 90 days. Read it slowly: an "expire after 90 days" rule applied to the whole bucket will also delete what you wanted to keep. If the summary says what you expected, Create rule.
Check after a few days
Rules run once a day, in the early hours, and the first pass can take more than a day to show. In the bucket's Metrics tab you will see the total size go down. If you have many objects, turn on the inventory or look at the S3 cost in Cost Explorer over the following weeks: it should drop and level off.
What can go wrong
Something I wanted to keep has been deleted
If the bucket had versioning and the previous versions have not expired yet, recover them (show versions and restore). If not, it is gone. Lesson: rule by prefix, not for the whole bucket, and a test first on a small folder.
The size does not go down
If the bucket has versioning and you only ticked "expire current versions", the files have become previous versions, which still take up space. Add the action to delete noncurrent versions. And give it a couple of days.
Recovering an archived file takes hours
That is the thing with archive classes: very cheap to store, slow and with a cost to retrieve. Use them only for what you are almost certain you will not need; everything else, in infrequent access.
I have dozens of buckets and do not know which ones are growing
In the S3 console, the list does not show the size. Cost Explorer grouped by S3 usage type, or the S3 Storage Lens dashboard (free in its basic version), tell you which buckets take up the most.
In Simplio
In Simplio every bucket shows its size and how much it has grown this month, for all your accounts, and alerts you if one grows suddenly. Automatic clean-up is set in two fields ("keep backups so many days, previous versions so many") and Simplio creates the right lifecycle rule, always including the clean-up of incomplete uploads.
The same task, in AWS and in Simplio
| In the AWS console | In Simplio | |
|---|---|---|
| Setting up a clean-up rule | S3 console, Management tab, 5 screens of technical options | On the bucket: "keep backups 90 days, versions 30" in two fields |
| Knowing which bucket is growing | Cost Explorer or Storage Lens | Size and monthly change of each bucket in the list, with an alert if one grows suddenly |
| Several accounts | Bucket by bucket, account by account | All buckets from all accounts on one screen |