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

Latest comments (0)

Timeless DEV post...

Git Concepts I Wish I Knew Years Ago

The most used technology by developers is not Javascript.

It's not Python or HTML.

It hardly even gets mentioned in interviews or listed as a pre-requisite for jobs.

I'm talking about Git and version control of course.

One does not simply learn git