Skip to content

Instantly share code, notes, and snippets.

@snobear
Last active May 8, 2023 19:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save snobear/546d707a1e0ca182c743d113560f15d5 to your computer and use it in GitHub Desktop.
Save snobear/546d707a1e0ca182c743d113560f15d5 to your computer and use it in GitHub Desktop.
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