Home AWSAWS S3 Lifecycle Rules: Cut Storage Costs Automatically

AWS S3 Lifecycle Rules: Cut Storage Costs Automatically

by Knowledge Base Linux
AWS S3 lifecycle rules moving cloud storage files from warm tiers to colder archive tiers over a timeline

Storage bills grow quietly: a bucket created for a short project keeps every version forever, logs accumulate by the gigabyte, and data nobody has opened in a year still sits in the most expensive tier. AWS S3 lifecycle rules fix this automatically by moving objects to cheaper storage classes as they age, expiring old versions, and deleting data you no longer need, all without a manual cleanup job.

AWS S3 lifecycle rules conveying old files into a deep archive vault while recent files stay accessible

This guide explains AWS S3 lifecycle rules in practical terms: transition and expiration actions, versioning behaviour, prefix and tag filters, cost pitfalls, and how to test a rule before it touches production data. If you serve a static site from S3, see our S3 static website guide, and for automated database backups, our RDS backups guide.

Prerequisites

  • An AWS account with permission to manage S3 buckets, and the AWS CLI configured or access to the S3 console.
  • A test bucket with versioning enabled, so you can see how lifecycle rules treat current and noncurrent versions differently.
  • A rough idea of your access pattern: which prefixes are logs, backups, or archives, and how long each should live.

AWS S3 Lifecycle Rules Basics: Transitions and Expiration

A lifecycle rule has a filter, a status, and one or more actions. The filter selects objects by prefix, tag, or both. Transition actions move objects to another storage class after a number of days; expiration actions delete objects or incomplete multipart uploads after a number of days. A typical log-retention rule moves logs to cheaper storage after 30 days and deletes them after a year:

{
  "Rules": [
    {
      "ID": "logs-retention",
      "Status": "Enabled",
      "Filter": { "Prefix": "logs/" },
      "Transitions": [
        { "Days": 30, "StorageClass": "STANDARD_IA" },
        { "Days": 90, "StorageClass": "GLACIER_IR" }
      ],
      "Expiration": { "Days": 365 },
      "AbortIncompleteMultipartUpload": { "DaysAfterInitiation": 7 }
    }
  ]
}

Apply a JSON rule file with the CLI, and list the rules back to confirm they landed. Always test on a non-production bucket first, because a mis-filtered expiration rule can delete data you intended to keep.

aws s3api put-bucket-lifecycle-configuration \
  --bucket my-test-bucket \
  --lifecycle-configuration file://lifecycle.json
aws s3api get-bucket-lifecycle-configuration --bucket my-test-bucket

Storage Classes and When to Use Each One

S3 Standard is for frequently accessed data. Standard-IA suits data accessed less than once a month but still needed quickly. Glacier Instant Retrieval is for rarely accessed data that still needs millisecond retrieval, Glacier Flexible Retrieval is for archives retrieved in minutes to hours, and Glacier Deep Archive is the cheapest option for data retrieved at most once or twice a year. The right tier is a trade-off between storage price, retrieval price, and retrieval time, not just the lowest monthly rate.

Two cost rules catch people out. First, objects smaller than 128 KB moved to Standard-IA or Glacier are still billed as 128 KB, so transitioning tiny files can cost more than leaving them in Standard. Second, Glacier classes have minimum storage durations: deleting or moving an object early still bills for the minimum period. Use S3 Intelligent-Tiering for data with unpredictable access patterns; it moves objects between tiers automatically based on actual access, at a small monitoring fee per object.

AWS S3 Lifecycle Rules with Versioning

Versioning changes the picture completely. A lifecycle rule’s expiration action applies to the current version, while noncurrent version actions apply to older versions after they are superseded. If you only set current-version expiration, old versions accumulate forever and your bill keeps growing. A complete versioned-bucket rule handles both:

{
  "ID": "versioned-cleanup",
  "Status": "Enabled",
  "Filter": { "Prefix": "" },
  "NoncurrentVersionTransitions": [
    { "NoncurrentDays": 30, "StorageClass": "STANDARD_IA" },
    { "NoncurrentDays": 90, "StorageClass": "GLACIER_IR" }
  ],
  "NoncurrentVersionExpiration": { "NoncurrentDays": 180 }
}

Also add an expired-object-delete-marker rule if you delete current versions but keep the delete markers; otherwise markers accumulate and listing costs grow. For buckets with many versions, run a lifecycle rule rather than a manual delete job: the service handles the scale, and you avoid API costs from listing millions of versions yourself.

Filtering by Prefix and Tag

Filters are how one bucket serves many purposes safely. Use prefixes to separate logs, uploads, and backups, and use tags when the same prefix holds data with different retention needs. A rule can combine a prefix and a tag, but the tag must match exactly, so tag consistently at upload time or with a batch tagging job.

aws s3api put-object-tagging \
  --bucket my-bucket --key uploads/report.pdf \
  --tagging 'TagSet=[{Key=retention,Value=short}]'

Document every rule’s purpose in its ID or in a bucket README: logs-365-days is self-explanatory, rule-3 is not. When someone asks why data disappeared, the rule ID and its filter are the first things you will check.

Troubleshooting AWS S3 Lifecycle Rules

Objects are not transitioning. Lifecycle runs once a day, so allow 24 to 48 hours before judging a rule. Also check that the rule is enabled, the filter matches the object key exactly, and the object is old enough: transition days count from object creation, and a 30-day rule does not move a 29-day-old object.

Costs went up after adding a rule. Check for small-object overhead, early-deletion charges on Glacier tiers, and retrieval fees from applications that still read archived data. Use S3 Storage Lens or Cost Explorer by storage class to see which tier is driving the bill.

Data disappeared unexpectedly. Check the bucket’s lifecycle rules, versioning state, and CloudTrail for delete events. If versioning was on, the data may still be recoverable as a noncurrent version; if it was off and expiration deleted it, recovery requires a backup outside S3.

Incomplete uploads keep costing money. Failed multipart uploads leave parts that count as storage. The AbortIncompleteMultipartUpload action in the example above cleans them after seven days; add it to every bucket that accepts large uploads.

AWS S3 Lifecycle Rules Best Practices

Start with a conservative rule on a test bucket, verify with a small prefix, then expand. Prefer Intelligent-Tiering for unpredictable data, keep versioned cleanup rules paired with current-version rules, abort incomplete uploads, and review Storage Lens monthly to confirm the rules are actually saving money. Lifecycle rules are set-and-forget only if the data patterns never change; in practice, a quarterly review keeps retention aligned with what the business actually needs.

AWS S3 lifecycle rules turn storage hygiene from a chore into a policy: define once how long each kind of data should live and where it should live, and let S3 enforce it every day. For the official reference, see the AWS S3 lifecycle documentation.

You may also like

KbLinux

Typically replies within a day

Hello, Welcome to the site. Please click below button for chatting me through Telegram.