Using Lifecycle Policies to Empty S3 Buckets

#aws #s3

There exists an AWS Knowledge Center post about emptying S3 buckets without API calls by using a LifeCycle Policy. Unfortunately the sample JSON syntax they provide is broken because it as an uppercase True in it. The following version of their JSON policy is correct:

{
  "Rules": [{
      "Expiration": {
        "Days": 1
      },
      "ID": "FullDelete",
      "Filter": {
        "Prefix": ""
      },
      "Status": "Enabled",
      "NoncurrentVersionExpiration": {
        "NoncurrentDays": 1
      },
      "AbortIncompleteMultipartUpload": {
        "DaysAfterInitiation": 1
      }
    },
    {
      "Expiration": {
        "ExpiredObjectDeleteMarker": true
      },
      "ID": "DeleteMarkers",
      "Filter": {
        "Prefix": ""
      },
      "Status": "Enabled"
    }
  ]
}

You can apply it to a bucket like so:

$ aws s3api put-bucket-lifecycle-configuration --lifecycle-configuration file://lifecycle.json --bucket your-bucket-name

Hope that helps!