Documentation

How to configure lifecycle policies

Enabling versioning usually result in additional storage costs since multiple versions of objects are stored. To help manage costs when using versioning, you can implement lifecycle policies to automatically expire (delete) versions after a certain period.

To create a lifecycle policy for a versioned bucket, you need to create a JSON file that defines the lifecycle rules. Here’s an example of a lifecycle policy JSON file (lifecycle.json) that expires non-current object versions after 30 days:

{
    "Rules": [
        {
            "ID": "DeleteOldVersions",
            "Status": "Enabled",
            "Prefix": "",
            "NoncurrentVersionExpiration": {
                "NoncurrentDays": 30
            }
        }
    ]
}

To apply this lifecycle policy to your versioned bucket, use the following command:

aws s3api put-bucket-lifecycle-configuration --bucket {bucket-name} --lifecycle-configuration file://{filepath-of-policy} --profile={profile}

aws s3api put-bucket-lifecycle-configuration --bucket vertest --lifecycle-configuration file://lifecycle_policy.json --profile=objectstorage-v2

You can confirm that the lifecycle policy has been applied by retrieving the bucket’s lifecycle configuration:

aws s3api get-bucket-lifecycle-configuration --bucket {bucket-name} --profile={profile}

aws s3api get-bucket-lifecycle-configuration --bucket vertest --profile=objectstorage-v2

{
    "Rules": [
        {
            "ID": "DeleteOldVersions",
            "Prefix": "",
            "Status": "Enabled",
            "NoncurrentVersionExpiration": {
                "NoncurrentDays": 30
            }
        }
    ]
}