"S3 cleans up old files on its own." It doesn't. Amazon S3 keeps every object you write until someone deletes it, and you pay for every gigabyte every month. Logs, exports, temporary files and old backups quietly add up. The tool that fixes this is an S3 Lifecycle configuration: a set of rules that tells S3 what to do with objects as they age.
What a lifecycle rule is
A lifecycle configuration belongs to one bucket and can hold up to 1,000 rules. Each rule has three parts:
- A filter that picks the objects: a key prefix (such as
logs/), one or more object tags, an object size range, or a combination. An empty filter means the whole bucket. - A status: enabled or disabled.
- One or more actions with a number of days (or a date).
Rules apply to existing objects too. Add a rule that expires objects after 30 days, and S3 queues everything already older than 30 days for removal. Lifecycle runs asynchronously, so actions can happen a little after the due date, but billing changes as soon as the object becomes eligible.
Transitions: move data to cheaper storage
Transition actions move objects down the storage-class "waterfall". A typical rule for data that cools off over time:
- Day 0: S3 Standard while the data is hot.
- Day 30: S3 Standard-IA (infrequent access).
- Day 90: S3 Glacier Flexible Retrieval for archive.
Transitions only go down the waterfall; to bring an archived object back to Standard you restore it and copy it. If you don't know the access pattern, a transition to S3 Intelligent-Tiering lets S3 move objects between tiers for you.
Expiration: delete on schedule
Expiration actions delete objects for you. "Delete anything under tmp/ after 7 days" or "delete access logs after 365 days" is one rule, with no script or cron job to maintain. Expiring objects doesn't cost anything extra unless they sit in a class with a minimum storage duration.
In a versioned bucket, expiration doesn't destroy data: it adds a delete marker and the old copy becomes a noncurrent version. To actually reclaim space, add a NoncurrentVersionExpiration action. While you're there, add AbortIncompleteMultipartUpload: parts of failed uploads are billed but invisible in a normal listing.
Traps to avoid
- Small objects don't transition by default. Since September 2024, objects under 128 KB are skipped unless you set an object size filter. Each transition is a billed request, so for tiny files it often costs more than it saves.
- Minimum storage durations. Standard-IA and One Zone-IA bill at least 30 days; Glacier Instant Retrieval and Glacier Flexible Retrieval 90 days; Glacier Deep Archive 180 days. Move data there and delete it a week later, and you still pay for the minimum.
- A bucket policy can't stop lifecycle. Even a policy that denies everything won't block a lifecycle deletion, so review rules carefully before enabling them on production data.
Where AI fits
AI applications write a lot of data nobody reads twice: prompts, responses, intermediate files and evaluation outputs. If you turn on Amazon Bedrock model invocation logging with an S3 destination, every request and response lands in your bucket. Put those logs under one prefix and give it an expiration rule, for example 30 days, unless compliance says to keep them longer. The same goes for batch inference outputs and scratch files from data pipelines: decide how long they live on the day you create the bucket, not when the bill arrives.
Quick recap
- A lifecycle rule = filter (prefix, tags, size) + transition and/or expiration actions.
- Transitions move objects to cheaper classes; expiration deletes them automatically.
- Watch the 128 KB default and minimum storage durations, and add noncurrent-version and multipart cleanup on versioned buckets.