Skip to content

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

Option 1 – Using AWS CLI

Step 1

export bucketname='your-bucket-here'
Code language: Bash (bash)

Step 2

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

Step 3

aws s3 rb s3://$bucketname
Code language: Bash (bash)

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()
Code language: Python (python)

See also  The Primary Data Types in Java
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x