An expiration (or lifecycle rule) tells the bucket to automatically delete or move an object after a set amount of time. You configure it once, and the storage service enforces it. This is housekeeping on autopilot. Old files clean themselves up so you don't have to remember.
How to use it
import { PutBucketLifecycleConfigurationCommand } from "@aws-sdk/client-s3";
// Tell the bucket to delete anything under "tmp/" 7 days after it's created
await s3.send(new PutBucketLifecycleConfigurationCommand({
Bucket: "my-app-uploads",
LifecycleConfiguration: {
Rules: [
{
ID: "expire-tmp",
Filter: { Prefix: "tmp/" },
Expiration: { Days: 7 },
Status: "Enabled",
},
],
},
})); You set this once per bucket; the storage service enforces it from then on.
Exercise
Code challenge
Build a tmp/ lifecycle rule
Build the lifecycle rule that deletes everything under the tmp/ prefix seven days after it's created. Fill in the prefix, the number of days, and the status that makes the rule active.