Bucket Lifecycle

The lifecycle configuration allow you to set an expiration policy on your objects and auto-delete them.

For example, you may need for some objects to be deleted automatically.

In this example, we will automatically create a policy to delete objects with a key starting with reports/ after 90 days. We could use a GUI Client or AWS SDK, but we will use AWS CLI to do so.

Limitations

  • Lifecycle is a bucket level concept.

  • Maximum of 1000 lifecycle rules per bucket is applicable.

  • There may be a delay between the expiration date and the date at which Object Storage Service removes an object.

  • Always round up the resulting time to the next day midnight UTC.

Deleted Object cannot be restored.

Manage lifecycle policy

Bucket lifecycle configuration can be managed using aws s3api (other tools or SDK works too):

  • put-bucket-lifecycle

  • get-bucket-lifecycle

  • delete-bucket-lifecycle

We use aws s3 and aws s3apicommand line tools from AWSCLIv2 on Linux.

${S3_ENDPOINT} & ${S3_PROFILE} are environment variables.

Create a lifecycle policy

Create JSON file and put your policy in it:

delete_after_3_days.json
{
  "Rules": [
    {
      "Filter": {
        "Prefix": ""
      },
      "Expiration": {
        "Days": 3
      },
      "Status": "Enabled",
      "ID": "Delete After 3 days."
    }
  ]
}

Apply it to the bucket: bucket-test

aws s3api --endpoint-url=${S3_ENDPOINT} put-bucket-lifecycle --bucket bucket-test --lifecycle-configuration file://delete_after_3days.json --profile ${S3_PROFILE}

Get a lifecycle configuration

aws s3api --endpoint-url=${S3_ENDPOINT} get-bucket-lifecycle --bucket bucket-test --profile ${S3_PROFILE}

Delete a lifecycle configuration

aws s3api --endpoint-url=${S3_ENDPOINT} delete-bucket-lifecycle --bucket bucket-test --profile ${S3_PROFILE}

Supported lifecycle configuration elements

Last updated