Created
July 1, 2013 12:48
-
-
Save madssj/5900474 to your computer and use it in GitHub Desktop.
Fast deletion of S3 bucket contents.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import boto | |
buckets = sys.argv[1:] | |
if "," in buckets: | |
buckets = buckets.split(",") | |
buckets = [bucket.strip() for bucket in buckets] | |
if not buckets: | |
sys.stderr.write("Usage: {0} <bucket> [[bucket] ...]\n".format(sys.argv[0])) | |
sys.exit(1) | |
try: | |
conn = boto.connect_s3() | |
except boto.exception.NoAuthHandlerFound: | |
sys.stderr.write("Error: boto failed to connect to s3, check AWS_ACCESS_KEY_ID and\n" | |
"AWS_SECRET_ACCESS_KEY env variables.") | |
raise | |
s3_buckets = [] | |
try: | |
for bucket_name in buckets: | |
bucket = conn.get_bucket(bucket_name) | |
s3_buckets.append(bucket) | |
except boto.exception.S3ResponseError: | |
sys.stderr.write("Error: bucket {0} not found or access denied\n".format(bucket_name)) | |
sys.exit(2) | |
for bucket in s3_buckets: | |
bucket.delete_keys(bucket.list(), quiet=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment