From f1d5e236bab34b4978fa3a7ee69d304a5d508f12 Mon Sep 17 00:00:00 2001 From: Ray Miller Date: Sat, 13 Jul 2024 15:04:53 +0100 Subject: [PATCH] Add script to delete AWS S3 bucket --- aws/delete_s3_bucket.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100755 aws/delete_s3_bucket.py diff --git a/aws/delete_s3_bucket.py b/aws/delete_s3_bucket.py new file mode 100755 index 0000000..6df0773 --- /dev/null +++ b/aws/delete_s3_bucket.py @@ -0,0 +1,35 @@ +#!/usr/bin/python3 +# +# An S3 bucket can only be deleted if it is empty, so all +# objects must be deleted. For a versioned bucket, this includes +# object versions and object deletion markers. +# + +import argparse +import boto3 +import click + + +def delete_s3_bucket(bucket_name, dry_run=True): + s3 = boto3.resource('s3') + bucket = s3.Bucket(bucket_name) + if not bucket.creation_date: + print(f"Bucket {bucket_name} not found") + return + n = 0 + for o in bucket.objects.all(): + n = n+1 + print(f"Delete {o.key}") + if click.confirm(f"Delete {n} objects from {bucket_name}?"): + bucket.objects.all().delete() + bucket.object_versions.all().delete() + bucket.delete() + print(f"Deleted bucket {bucket_name}") + + +if __name__ == "__main__": + parser = argparse.ArgumentParser(description="Delete S3 bucket and its contents") + parser.add_argument("bucket", help="Name of the bucket to delete", nargs=1) + args = parser.parse_args() + for bucket in args.bucket: + delete_s3_bucket(bucket)