Applying lifecycle policies

Last change on 2025-04-08 • Created on 2024-10-28 • ID: ST-B7A33

To automatically delete objects after a set time period, you can use lifecycle policies. For more information about lifecycle policies, see the FAQ entry "What are lifecycle policies and how do I use them?".

The commands depend on the S3-compatible tool you're using. This getting started explains each step with example commands for the MinIO Client and the AWS CLI with a JSON file. For S3cmd, use an XML file and see the official S3cmd documentation.

Warning: At the moment, lifecycle policies do NOT work if versioning is suspended on the Bucket. For lifecycle policies to work, the Bucket should either have no versioning at all, or versioning should be enabled.

  1. Create a lifecycle configuration, e.g. expiry.json:

    (see JSON Syntax)

    {
     "Rules": [{
        "ID": "expiry",
        "Status": "Enabled",
        "Prefix": "",
    
        "Expiration": {
          "Days": 90
        },
        "NoncurrentVersionExpiration": {
          "NoncurrentDays": 30
        },
        "AbortIncompleteMultipartUpload": {
          "DaysAfterInitiation": 10
        }
      },
      {
        "ID": "deletemarker",
        "Status": "Enabled",
        "Prefix": "",
    
        "Expiration": {
          "ExpiredObjectDeleteMarker": true
        }
      }]
    }

    The configuration above will perform the following deletions:

    Option Description
    Expiration: Days 90 days after an object was created, it is either permanently deleted (no versioning), or a delete marker is added (versioning enabled).
    NoncurrentVersionExpiration Note that our Object Storage only supports the option NoncurrentDays for this data type.

    30 days after an object became a "noncurrent version" (replaced by a newer version), it is permanently deleted — unless object lock is applied.
    AbortIncompleteMultipartUpload 10 days after a multipart upload was aborted, the "leftover" parts are automatically deleted.
    ExpiredObjectDeleteMarker If a delete marker is the only remaining version of an object and all noncurrent versions have been permanently deleted, the delete marker is also deleted.

  1. Apply the lifecycle configuration to your Bucket:

    • MinIO Client

      With the MinIO Client, you also have the option to set lifecycle rules directly without a JSON file.

      mc ilm rule import <alias_name>/<bucket_name> < expiry.json

    • AWS CLI

      aws s3api put-bucket-lifecycle-configuration --bucket <bucket_name> --lifecycle-configuration  file://expiry.json

  1. View the lifecycle rules:

    • MinIO Client

      mc ilm rule ls <alias_name>/<bucket_name>

    • AWS CLI

      aws s3api get-bucket-lifecycle-configuration --bucket <bucket_name>

  1. Remove the lifecycle rules:

    • MinIO Client

      mc ilm rule rm --id "expiry" <alias_name>/<bucket_name>
      mc ilm rule rm --all --force <alias_name>/<bucket_name>

    • AWS CLI

      Delete all rules:

      aws s3api delete-bucket-lifecycle --bucket <bucket_name>

      If you want to delete individual rules, you can save the current rules in lifecycle.json with the command below, edit the rules, and apply the new lifecycle configuration as explained in step 2.

      aws s3api get-bucket-lifecycle-configuration --bucket <bucket_name> --output json > lifecycle.json

As long as the lifecycle rules are applied, you're objects should get deleted automatically.


Next: