Skip to content

Instantly share code, notes, and snippets.

@raymondberg
Created May 14, 2019 17:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raymondberg/91f3148a57d85258d2f0d53c04a6e1f4 to your computer and use it in GitHub Desktop.
Save raymondberg/91f3148a57d85258d2f0d53c04a6e1f4 to your computer and use it in GitHub Desktop.
Delete All Versions of S3 Objects based on bucket and prefix
import sys
import boto3
def delete_all_version_fuzzy(bucket_name, prefix, do_it=False):
s3 = boto3.resource('s3')
bucket = s3.Bucket(bucket_name)
count = 0
print("Searching bucket({}) for prefix({})".format(bucket.name, prefix))
for version in bucket.object_versions.filter(Prefix=prefix):
count += 1
if do_it:
version.delete()
print("Deleted {} versions {}".format(count, "were deleted" if do_it else "would be deleted"))
if __name__ == '__main__':
if len(sys.argv) not in (3, 4):
print("usage: delete_them_all.py bucket prefix [-y]")
exit(1)
delete_all_version_fuzzy(sys.argv[1], sys.argv[2], len(sys.argv) == 4 and sys.argv[3] == '-y')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment