Skip to content

Instantly share code, notes, and snippets.

@MrZoidberg
Created October 3, 2025 14:50
Show Gist options
  • Save MrZoidberg/0616a3bb634a09b8ef3d954df8c338b2 to your computer and use it in GitHub Desktop.
Save MrZoidberg/0616a3bb634a09b8ef3d954df8c338b2 to your computer and use it in GitHub Desktop.
Python script to empty and delete s3 bucket
import argparse
import sys
import boto3
import botocore
def delete_s3_bucket(profile_name: str, bucket_name: str, force: bool = False):
# Use the provided AWS CLI profile
session = boto3.Session(profile_name=profile_name)
s3 = session.resource("s3")
bucket = s3.Bucket(bucket_name)
try:
if not force:
confirm = input(f"⚠️ Are you sure you want to delete bucket '{bucket_name}' and all its contents? [y/N]: ").strip().lower()
if confirm not in ["y", "yes"]:
print("Aborted.")
return
print(f"Emptying bucket: {bucket_name} ...")
bucket.objects.all().delete()
bucket.object_versions.all().delete() # Handles versioned buckets
print(f"Deleting bucket: {bucket_name} ...")
bucket.delete()
print(f"✅ Bucket '{bucket_name}' has been deleted successfully.")
except botocore.exceptions.ClientError as e:
print(f"❌ Error: {e}")
sys.exit(1)
def main():
parser = argparse.ArgumentParser(
description="Empty and delete an S3 bucket using a specified AWS profile."
)
parser.add_argument(
"-p", "--profile",
required=True,
help="AWS CLI profile name"
)
parser.add_argument(
"-b", "--bucket",
required=True,
help="S3 bucket name to delete"
)
parser.add_argument(
"-f", "--force",
action="store_true",
help="Skip confirmation prompt"
)
args = parser.parse_args()
delete_s3_bucket(args.profile, args.bucket, args.force)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment