Applying lifecycle policies

Last change on 2024-12-03 • 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.

  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
        }
      }]
    }

  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

You're objects should now get deleted automatically.


Next: