Sub topic 12
Automating transitions and expiration with lifecycle rules
A lifecycle configuration tells S3 to move objects to cheaper storage classes or delete them as they age, so you don't have to script that work. This chapter assumes you know what a storage class is, how keys and prefixes work, and what versioning does. It also assumes you can run aws s3api commands against a bucket you own.
What a lifecycle rule is
A lifecycle configuration belongs to one bucket and holds up to 1,000 rules. Each rule has three parts:
- A status, either
EnabledorDisabled. - A filter that selects which objects the rule applies to.
- One or more actions that say what happens to those objects and when.
Lifecycle actions fall into two groups:
- Transition actions move objects to another storage class after a number of days.
- Expiration actions delete objects, or in a versioned bucket, create delete markers.
S3 measures age in days since the object was created. For noncurrent versions, it measures days since the version stopped being current.
A first rule: archive and then expire logs
This configuration moves objects under logs/ to Standard-IA after 30 days and to Glacier Flexible Retrieval after 90 days. It deletes them after 365 days.
{
"Rules": [
{
"ID": "archive-then-expire-logs",
"Status": "Enabled",
"Filter": { "Prefix": "logs/" },
"Transitions": [
{ "Days": 30, "StorageClass": "STANDARD_IA" },
{ "Days": 90, "StorageClass": "GLACIER" }
],
"Expiration": { "Days": 365 }
}
]
}Save it as lifecycle.json and apply it:
aws s3api put-bucket-lifecycle-configuration \
--bucket my-example-bucket-123456789012 \
--lifecycle-configuration file://lifecycle.json
aws s3api get-bucket-lifecycle-configuration \
--bucket my-example-bucket-123456789012put-bucket-lifecycle-configuration replaces the whole configuration. It does not add a rule to the existing set. To add a rule, run get-bucket-lifecycle-configuration, edit the JSON, and put the full document back.
The GLACIER value in the API means Glacier Flexible Retrieval. Glacier Instant Retrieval is GLACIER_IR, and Deep Archive is DEEP_ARCHIVE.
How the timeline plays out
The Days values are counted from the object's creation date, not from the previous transition. The day 90 transition happens 60 days after the day 30 one.
Filters
The Filter element selects objects in one of these ways:
| Filter | Matches |
|---|---|
| `{ "Prefix": "logs/" }` | Keys that start with `logs/` |
| `{ "Tag": { "Key": "retain", "Value": "short" } }` | Objects carrying that tag |
| `{ "ObjectSizeGreaterThan": 131072 }` | Objects larger than the given number of bytes |
| `{ "ObjectSizeLessThan": 1048576 }` | Objects smaller than the given number of bytes |
| `{ "And": { ... } }` | Objects that match all the listed conditions |
| `{}` | Every object in the bucket |
A filter with two or more conditions must wrap them in And. An empty filter applies the rule to the whole bucket, so check it before enabling. A prefix matches on the key string, so logs also matches logs-archive/. Include the trailing slash when you mean a folder-like prefix.
Rules you must respect
S3 enforces several constraints on transitions:
- Direction is one-way. Lifecycle can move objects only down the waterfall: Standard, Standard-IA, Intelligent-Tiering, One Zone-IA, Glacier Instant Retrieval, Glacier Flexible Retrieval, Deep Archive. It cannot move an object back up. To get an object back to Standard, restore it or copy it.
- Minimum age. Transitions from Standard to Standard-IA or One Zone-IA require the object to be at least 30 days old.
- Small objects. By default, S3 does not transition objects smaller than 128 KB to another storage class. Each transitioned object adds metadata overhead and a per-request fee, so tiny objects can cost more in the colder class. You can override this default with an
ObjectSizeGreaterThanfilter. - Minimum storage duration. Standard-IA and One Zone-IA bill for at least 30 days, Glacier Instant and Flexible Retrieval for 90 days, and Deep Archive for 180 days. If an expiration rule deletes an object before that period ends, you pay a prorated early-deletion charge for the remainder.
- Retrieval. Objects in Glacier Flexible Retrieval and Deep Archive cannot be read directly. You must start a restore and wait for it to finish.
Transitions are billed per 1,000 requests, and the rate depends on the target class. A rule that transitions millions of small objects can cost a surprising amount. Expiration requests are free.
Lifecycle in versioned buckets
Versioning changes what expiration means. Each object has one current version and any number of noncurrent versions. The two kinds need separate actions.
| Action | Effect in a versioned bucket |
|---|---|
| `Expiration` with `Days` | Adds a delete marker as the new current version. The old data becomes noncurrent and is kept. |
| `NoncurrentVersionTransitions` | Moves noncurrent versions to a cheaper class after `NoncurrentDays`. |
| `NoncurrentVersionExpiration` | Permanently deletes noncurrent versions after `NoncurrentDays`. `NewerNoncurrentVersions` keeps that many recent noncurrent versions. |
| `Expiration` with `ExpiredObjectDeleteMarker` | Removes a delete marker when no noncurrent versions remain beneath it. |
This is a typical rule for a versioned bucket:
{
"Rules": [
{
"ID": "trim-old-versions",
"Status": "Enabled",
"Filter": { "Prefix": "reports/" },
"NoncurrentVersionTransitions": [
{ "NoncurrentDays": 30, "StorageClass": "GLACIER_IR" }
],
"NoncurrentVersionExpiration": {
"NoncurrentDays": 180,
"NewerNoncurrentVersions": 3
},
"Expiration": { "ExpiredObjectDeleteMarker": true }
}
]
}The rule keeps the three newest noncurrent versions and deletes older ones after 180 days. It also cleans up delete markers that no longer protect anything. Without the delete-marker cleanup, a bucket full of deleted objects still accumulates markers, and they slow down ListObjectsV2 calls.
A rule with Expiration set to Days and no noncurrent-version action never frees storage in a versioned bucket. The data stays as noncurrent versions and you keep paying for it.
Cleaning up incomplete multipart uploads
A multipart upload that is started but never completed leaves parts in the bucket. You are billed for those parts, and they do not appear in a normal object listing. Add this action to a rule with an empty filter, or put it in its own rule:
{
"ID": "abort-stale-uploads",
"Status": "Enabled",
"Filter": {},
"AbortIncompleteMultipartUpload": { "DaysAfterInitiation": 7 }
}This action can't be combined with a tag-based filter.
Timing and verification
S3 evaluates lifecycle rules once a day, and it rounds the scheduled action up to the next midnight UTC. A transition set for 30 days therefore happens on the first midnight UTC after the object turns 30 days old, and the actual change can take some hours more. Changes to a configuration can also take a few minutes to take effect.
An object that has passed its expiration date stops incurring storage charges even if S3 has not yet removed it. To check what a rule will do to a specific object, call head-object:
aws s3api head-object \
--bucket my-example-bucket-123456789012 \
--key logs/2026/app.logThe response includes an Expiration field with the expiry date and the matching rule ID. It includes StorageClass once the object has moved out of Standard.
Interactions to plan for
- Replication. Lifecycle actions are not replicated. Delete markers created by lifecycle expiration in the source bucket are not copied to the destination, so configure lifecycle on each bucket.
- Object Lock. Lifecycle cannot delete an object version while its retention period or legal hold is in effect.
- Existing objects. A new or changed rule applies to objects already in the bucket, not only to new ones. Applying
Expirationwith a filter of{}to a bucket full of old data can delete a large part of it on the next daily run.
Common errors
- Expecting a transition to happen exactly on day 30. It happens at the next midnight UTC boundary, with some delay.
- Overwriting a configuration with
put-bucket-lifecycle-configurationand losing earlier rules. - Writing
Prefixat the top level of a rule, which is the legacy format. UseFilter. - Forgetting noncurrent-version actions in a versioned bucket.
- Transitioning millions of small objects and paying more in request fees than the colder class saves.
Exercise
A versioned bucket has a rule with only "Expiration": { "Days": 90 } on the prefix tmp/. After 90 days, the team finds that the storage bill has not gone down. Why, and what would you add?
Show answer
In a versioned bucket, expiration of the current version only adds a delete marker. The previous data becomes a noncurrent version and is still stored and billed. Add NoncurrentVersionExpiration (for example NoncurrentDays: 7) so the noncurrent versions are permanently deleted, and add ExpiredObjectDeleteMarker: true to remove the leftover delete markers.
Exercise
Write the Filter for a rule that applies only to objects under backups/ that are larger than 1 MiB (1,048,576 bytes).
Show answer
{"And": {"Prefix": "backups/", "ObjectSizeGreaterThan": 1048576}}. Two conditions in one filter must be wrapped in And.