AWS Builder Center

S3 Lifecycle rules: what they delete, what they move, and where the costs hide

Study notes on how S3 Lifecycle expiration and transition actions behave, which cost traps the AWS documentation warns about, and a small exercise you can run on a free-plan account.

Why this topic is worth an hour

An S3 bucket keeps whatever you put in it until you remove it. Practice buckets collect test uploads, old versions and half-finished uploads, and all of it can count against your credits. Lifecycle rules automate the cleanup. These notes cover what a rule does and the cases where it surprises people. They come from the AWS documentation, and I have not run these tests myself.

The two kinds of action

To manage the lifecycle of objects, you create an S3 Lifecycle configuration for the bucket. Each configuration holds rules, and each rule contains one of two kinds of action:
  • Transition actions define when objects move to another storage class.
  • Expiration actions define when objects expire, and Amazon S3 deletes expired objects on your behalf.
If a rule is disabled, Amazon S3 does not perform any of its actions. Disabling a rule is a safe way to pause it while you debug.

Expiration depends on bucket versioning

The same Expiration action does different things depending on versioning. In a nonversioned bucket, Expiration permanently removes the object. In a versioned bucket, the docs describe separate handling for current and noncurrent versions. For that reason I would read the versioning table on the "Lifecycle configuration elements" page before enabling a rule on any versioned bucket. The same page says expiration can also remove expired object delete markers.

A new rule applies to existing objects

Many people expect a rule to affect only future uploads. The docs give an example. If you add a rule today that expires objects 30 days after creation, S3 queues for removal any existing objects older than 30 days. Test a new rule on a prefix such as  lifecycle-test/  first, not on the whole bucket.

Billing starts at eligibility, not at execution

Lifecycle actions run asynchronously, so a bucket listing may still show objects that are due to expire. The docs say billing changes apply as soon as the object becomes eligible for the action. For an object scheduled to expire, you are not charged for storage after the expiration time, even if S3 has not removed it yet.

Transition traps

Transitions are where students tend to lose money. Four points from the documentation:
  1. Requests cost money. There are costs associated with lifecycle transition requests. The details are on the S3 pricing page.
  1. Small objects are skipped by default. S3 prevents objects smaller than 128 KB from being transitioned to any storage class by default, because you are charged a transition request for each object. This default applies from September 2024. If your test files are tiny, a transition rule may do nothing, and that is intended.
  1. Minimum storage durations apply. If you transition objects out of a class before its minimum duration, you are charged for the remainder of that duration. The documented minimums are 90 days for S3 Glacier Flexible Retrieval and 180 days for S3 Glacier Deep Archive.
  1. Archive classes add per-object overhead. The docs list 40 KB per object, with 8 KB charged at S3 Standard rates and 32 KB at the Glacier rate. Many small objects can therefore cost more after archiving.

Overlapping rules

You may end up with several rules on one bucket. S3 Lifecycle generally optimizes for cost. If two expiration policies overlap, the shorter one is honored. If two transition policies overlap, objects go to the lower-cost class. If one rule transitions objects and another expires them at the same time, S3 chooses expiration.

Incomplete multipart uploads need their own rule

This one is easy to miss. The AWS docs note that object expiration configurations do not remove incomplete multipart uploads. Those leftover parts are still stored. AWS's cost management blog shows a dedicated rule for this. It uses a rule that aborts incomplete multipart uploads after 7 days, which also deletes the in-progress parts. It adds that the rule does not delete successfully assembled objects.
A re:Post answer says Lifecycle has no cost on its own, and DELETE and CANCEL actions are free per the pricing page. That is a community answer, so check the pricing page yourself.

Exercise: watch a rule expire a test object

The aim is to see an expiration rule work on a safe prefix. I would do this in order:
  1. Create a new bucket with a unique name and leave Block Public Access on.
  1. Upload two or three tiny text files under the prefix  lifecycle-test/ . Keep them very small so storage is negligible.
  1. Under Management, create a Lifecycle rule scoped to the prefix  lifecycle-test/ . Add an expiration action with the smallest day value the console allows, which I expect to be 1 day. Confirm that in the console.
  1. Add a second action in the same rule to abort incomplete multipart uploads after 7 days.
  1. Check the bucket after a day or two. Because actions run asynchronously, allow extra time. Note that the docs say billing stops at eligibility, not at removal.
What it could cost. The files are tiny, so storage should be a fraction of a cent at most. I did not find a specific figure for this, so look at the S3 pricing page for your Region. Under the current model, new AWS customers receive up to $200 in Free Tier credits, and the free plan lasts 6 months after account creation. Credits must be used within 12 months. The billing docs say you receive USD $100 in credits at sign-up and can earn up to another USD $100 by completing activities. The two pages give slightly different descriptions of the credits, so check your own Billing console for the actual balance.
Clean up. Delete the objects, then delete the bucket. Also delete or disable the Lifecycle rule if you keep the bucket. Afterward, check the Billing console for any S3 line items.
I would not add a transition rule in this exercise. The 128 KB default means tiny files would not move anyway, and transition requests are a charged action. Try a transition only after reading the pricing page.

Sources

Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article