Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Expire all objects in an S3 bucket with a lifecycle rule using aws cli
#!/bin/bash
# set an s3 bucket to expire after one day. Faster than waiting to delete all the files.
# check back after a day or so (depending on size of bucket) and delete the bucket from s3
if [ "$#" -lt 2 ]; then
echo "error: run_report expects at least 2 arguments; got $#."
echo "Usage:"
echo " ./$(basename $0) region bucket"
exit 1
fi
aws_region=$1
s3_bucket=$2
rm -f lifecycle-expire.json
cat << EOF > lifecycle-expire.json
{
"Rules": [{
"ID": "cleanup",
"Status": "Enabled",
"Prefix": "",
"Expiration": {
"Days": 1
}
}]
}
EOF
echo "Added lifecycle policy on S3 bucket $s3_bucket to expire objects after one day."
aws s3api put-bucket-lifecycle --bucket $s3_bucket --region $aws_region --lifecycle-configuration file://lifecycle-expire.json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment