DEV Community

Wesley Cheek
Wesley Cheek

Posted on

Python Script to Quickly Delete S3 Buckets

I got frustrated having to empty and delete S3 buckets using the AWS Console, so I created a Python script to do it for me!

WARNING: This method bypasses all of the usual safety associated with deleting buckets with the AWS Console, so make absolutely sure you want to delete your bucket before running this script.

# Use the boto3 library to communicate with AWS
import boto3

# Buckets you want to delete, specified by their names
BUCKETS = [
    "bucket-one",
    "bucket-two",
]

s3 = boto3.resource("s3")
for bucket_name in BUCKETS:
    # For each bucket, create a resource object with boto3
    bucket = s3.Bucket(bucket_name)
    # Delete all of the objects in the bucket
    bucket.object_versions.delete()

    # Delete the bucket itself!
    bucket.delete()
Enter fullscreen mode Exit fullscreen mode

Top comments (0)