Skip to content

Instantly share code, notes, and snippets.

@pwmcintyre
Last active September 23, 2022 02:28
Show Gist options
  • Save pwmcintyre/0259f9673a20a4b23cf2845d85f40026 to your computer and use it in GitHub Desktop.
Save pwmcintyre/0259f9673a20a4b23cf2845d85f40026 to your computer and use it in GitHub Desktop.
purge an s3 bucket of all it's contents, including all object versions
#!/usr/bin/env python
# $ nohup python3 ./purge.py foo-bucket &
import sys
import boto3
# Take the bucket name from command line args
BUCKET = sys.argv[1]
s3 = boto3.resource('s3')
bucket = s3.Bucket(BUCKET)
# Delete all object versions in the bucket
bucket.object_versions.delete()
# This is useful for long-running scripts, as your IAM Token will expire, or something else will break
# $ nohup bash z_keepalive.sh &
while true
do
python3 ./purge.py foo-bucket
sleep 1
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment