S3 Lifecycle Policy Examples

A lifecycle configuration is a set of rules that defines the actions ARTESCA applies to objects during their lifetime. This page provides ready-to-use JSON configurations for the most common lifecycle rules.

Lifecycle Configuration Format

The Amazon S3 lifecycle configuration is an XML document. The AWS CLI does not accept XML for this operation, so every example on this page uses the equivalent JSON form.

The following AWS CLI commands manage lifecycle configurations:

  • put-bucket-lifecycle-configuration replaces the whole configuration on a bucket.

  • get-bucket-lifecycle-configuration returns the configuration currently set on a bucket.

  • delete-bucket-lifecycle removes the configuration from a bucket.

Apply a Lifecycle Configuration

Note

You must have the proper IAM permissions when using this API as an IAM user.

  1. Write a policy in a JSON file. Refer to Expiration Examples for more information.

  2. Apply the file to the bucket.

    aws --endpoint-url http://endpoint s3api put-bucket-lifecycle-configuration \
      --bucket <BUCKET_NAME> --lifecycle-configuration file://path/to/policy/file
    

Expiration Examples

Each example is a complete configuration and replaces any rules already set on the bucket.

Expire Objects Under a Prefix and With a Tag

This rule deletes the current version of every object that both sits under the myprefix prefix and carries the tag mytagkey1=mytagvalue1, 1 day after creation.

{
    "Rules": [
        {
            "ID": "id-1",
            "Filter": {
                "And": {
                    "Prefix": "myprefix",
                    "Tags": [
                        {
                            "Value": "mytagvalue1",
                            "Key": "mytagkey1"
                        }
                    ]
                }
            },
            "Status": "Enabled",
            "Expiration": {
                "Days": 1
            }
        }
    ]
}

Abort Incomplete Multipart Uploads

This rule removes multipart uploads that are still incomplete 1 day after they started, which reclaims the storage their uploaded parts occupy.

{
  "Rules": [
    {
      "ID": "Simple_MPU",
      "Status": "Enabled",
      "Prefix": "",
      "AbortIncompleteMultipartUpload": {
        "DaysAfterInitiation": 1
      }
    }
  ]
}

Expire Noncurrent Versions and Expired Delete Markers

This rule deletes noncurrent object versions 1 day after they become noncurrent, and removes the delete markers left behind once every version of an object has expired.

{
    "Rules": [
        {
            "ID": "Expire",
            "Status": "Enabled",
            "Prefix": "",
            "NoncurrentVersionExpiration": {
                "NoncurrentDays": 1
            },
            "Expiration": {
                "ExpiredObjectDeleteMarker": true
            }
        }
    ]
}

Note

NoncurrentVersionExpiration has no effect on a bucket without versioning, and it never removes the current version of an object.