How to Empty and Delete an S3 Bucket Using the AWS CLI

Option 1 – Using AWS CLI

Step 1

export bucketname='your-bucket-here'

Step 2

aws s3api delete-objects --bucket $bucketname --delete "$(aws s3api list-object-versions --bucket $bucketname --output=json --query='{Objects: *[].{Key:Key,VersionId:VersionId}}')";

Step 3

aws s3 rb s3://$bucketname

Option 2 – Using Python

#!/usr/bin/env python

BUCKET = 'your-bucket-here'

import boto3

s3 = boto3.resource('s3')
bucket = s3.Bucket(BUCKET)
bucket.object_versions.delete()

bucket.delete()