Skip to content

Instantly share code, notes, and snippets.

@LyleScott
Last active June 15, 2024 11:22
Show Gist options
  • Save LyleScott/ab20b5969a441b2ebe8a28602b5deaee to your computer and use it in GitHub Desktop.
Save LyleScott/ab20b5969a441b2ebe8a28602b5deaee to your computer and use it in GitHub Desktop.
Python script to nuke a S3 Bucket - all files and object versions; supports regions and profiles
#!/usr/bin/env python3
"""
Lyle Scott, III // lyle@ls3.io
$ python nuke_s3_bucket.py --help
usage: nuke_s3_bucket.py [-h] [-p PROFILE] bucket_name
positional arguments:
bucket_name The bucket name to delete.
optional arguments:
-h, --help show this help message and exit
-p PROFILE, --profile PROFILE
Use a specific profile for bucket operations. Default:
"default" profile in ~/.aws/config or AWS_PROFILE
environment variable
$ python3 nuke_s3_bucket.py bucket-name-goes-here
Deleting bucket-name-goes-here ...done
$ python3 nuke_s3_bucket.py bucket-name-goes-here
Deleting bucket-name-goes-here ...error: {'Code': 'NoSuchBucket', 'Message': 'The specified bucket does not exist', 'BucketName': 'bucket-name-goes-here'}
NOTE: S3 buckets don't need a region to be deleted; they are account global.
"""
from __future__ import print_function
import argparse
import sys
import boto3
from botocore.exceptions import ClientError
def delete_bucket(bucket_name, profile=None):
"""Delete a bucket (and all object versions)."""
kwargs = {}
if profile:
kwargs['profile_name'] = profile
session = boto3.Session(**kwargs)
print('Deleting {} ...'.format(bucket_name), end='')
try:
s3 = session.resource(service_name='s3')
bucket = s3.Bucket(bucket_name)
bucket.object_versions.delete()
bucket.delete()
except ClientError as ex:
print('error: {}'.format(ex.response['Error']))
sys.exit(1)
print('done')
def _parse_args():
"""A helper for parsing command line arguments."""
parser = argparse.ArgumentParser()
parser.add_argument('bucket_name', help='The bucket name to delete.')
parser.add_argument('-p', '--profile', default='',
help='Use a specific profile for bucket operations. '
'Default: "default" profile in ~/.aws/config or '
'AWS_PROFILE environment variable')
return parser.parse_args()
def _main():
"""Script execution handler."""
args = _parse_args()
delete_bucket(args.bucket_name, profile=args.profile)
if __name__ == '__main__':
_main()
@davidawcloudsecurity
Copy link

davidawcloudsecurity commented Jun 15, 2024

This is so much better than running aws s3 delete API object with key and version
Thanks,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment