Here’s how to add a lifecycle management to S3 buckets via the CLI. In this example, expiration is set to 7 days. S3 will auto delete expired objects.

<pre lang="bash">$aws s3api put-bucket-lifecycle-configuration \
--bucket bucketname --lifecycle-configuration file://lifecycle.json

The lifecycle is defined in this JSON file.

<pre lang="bash">{
  "Rules": [ 
    {
       "ID": "Expires in 7 DAYS", 
       "Prefix": "", 
       "Status": "Enabled", 
       "Expiration": { 
         "Days": 7
         }
    } 
  ] 
}