Skip to content

Instantly share code, notes, and snippets.

@jiamaozheng
Last active May 3, 2018 23:06
Show Gist options
  • Save jiamaozheng/c90abc159327283fdc6fa855ed0e5053 to your computer and use it in GitHub Desktop.
Save jiamaozheng/c90abc159327283fdc6fa855ed0e5053 to your computer and use it in GitHub Desktop.
This script is used to delete the versioned bucket.
#!/usr/bin/env python
# usages: python delete_the_versioned_s3_bucket.py -b <YOUR BUCKET NAME>
import boto3, argparse, sys
def delete_the_versioned_s3_bucket():
# setup commond line arguments
parser = argparse.ArgumentParser()
# required arguments
parser.add_argument('-b', '--bucket_name', required=True, default=None, type=str, help='bucket name')
# parse the arguments
bucket = boto3.resource('s3').Bucket(parser.parse_args().bucket_name)
bucket.object_versions.delete()
# if you want to delete the now-empty bucket
bucket.delete()
# initialize the script
if __name__ == '__main__':
sys.exit(delete_the_versioned_s3_bucket())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment