Skip to content

Object Lifecycle Policy Examples

Estimated time to read: 2 minutes

On this page the lifecycle Rule Filters and Rule Actions are explained. Also with those there have been created some examples. It is possible to combine such rules to have multiple rules in place.

Lifecycle Management

Lifecycle Rule Filters

There are several filter options to use in a lifecycle policy. Below are some

Filter Option Description
AND operator Combine multiple conditions, like Prefix and Tags, into one rule.
Prefix Filters objects based on their prefix (e.g., logs/). Targets objects in specific folders.
ObjectSizeLessThan Filters objects smaller than a specified size (e.g., 100MB).
ObjectSizeGreaterThan Filters objects larger than a specified size (e.g., 100MB).
Tag Filters objects based on tags, e.g., Key=category, Value=backup.

Lifecycle Rule Actions

Lifecycle rules allow you to automate object management. The available actions include:

Action Description
AbortMultipartUploads Automatically clean up incomplete multipart uploads after a specified number of days.
Expiration Automatically delete objects after a specified number of days.
ExpiredObjectDeleteMarker Remove expired object delete markers (created after expiration) in versioned buckets.
NoncurrentVersionExpiration Remove older versions of objects if versioning is enabled.

Examples

Delete Objects Older Than 180 Days

This rule automatically deletes objects that are older than 180 days.

{
  "Rules": [
    {
      "ID": "DeleteAfter180Days",
      "Prefix": "",
      "Status": "Enabled",
      "Expiration": {
        "Days": 180
      }
    }
  ]
}

Delete Objects with a Specific Prefix After 30 Days

This rule automatically deletes objects in the logs/ folder after 30 days.

{
  "Rules": [
    {
      "ID": "DeleteLogsAfter30Days",
      "Prefix": "logs/",
      "Status": "Enabled",
      "Expiration": {
        "Days": 30
      }
    }
  ]
}

Delete Old Versions (Versioning Enabled)

If versioning is enabled, this rule deletes noncurrent versions of objects after 90 days.

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

Delete Objects with a Specific Tag

This rule deletes objects tagged as "temporary" after 7 days.

{
  "Rules": [
    {
      "ID": "DeleteTemporaryFiles",
      "Filter": {
        "Tag": {
          "Key": "type",
          "Value": "temporary"
        }
      },
      "Status": "Enabled",
      "Expiration": {
        "Days": 7
      }
    }
  ]
}

Abort Incomplete Multipart Uploads After 5 Days

This rule automatically cleans up incomplete multipart uploads after 5 days.

{
  "Rules": [
    {
      "ID": "AbortMultipartUploads",
      "Prefix": "",
      "Status": "Enabled",
      "AbortIncompleteMultipartUpload": {
        "DaysAfterInitiation": 5
      }
    }
  ]
}

Delete Large Objects After 365 Days

This rule deletes objects larger than 100 MB after 365 days.

{
  "Rules": [
    {
      "ID": "DeleteLargeFiles",
      "Filter": {
        "ObjectSizeGreaterThan": 104857600
      },
      "Status": "Enabled",
      "Expiration": {
        "Days": 365
      }
    }
  ]
}

Delete Files Matching a Specific Prefix and Tag

This rule deletes objects with the "cache/" prefix and tagged as "stale" after 15 days.

{
  "Rules": [
    {
      "ID": "DeleteStaleCache",
      "Filter": {
        "And": {
          "Prefix": "tmp/",
          "Tags": [
            {
              "Key": "status",
              "Value": "old"
            }
          ]
        }
      },
      "Status": "Enabled",
      "Expiration": {
        "Days": 15
      }
    }
  ]
}