Explore Courses Blog Tutorials Interview Questions
0 votes
in AWS by (12.9k points)

I have tried both s3cmd:

$ s3cmd -r -f -v del s3://my-versioned-bucket/

And the AWS CLI:

$ aws s3 rm s3://my-versioned-bucket/ --recursive

But both of these commands simply add DELETE markers to S3. The command for removing a bucket also doesn't work (from the AWS CLI):

$ aws s3 rb s3://my-versioned-bucket/ --force

Cleaning up. Please wait...

Completed 1 part(s) with ... file(s) remaining

remove_bucket failed: s3://my-versioned-bucket/ A client error (BucketNotEmpty) occurred when calling the DeleteBucket operation: The bucket you tried to delete is not empty. You must delete all versions in the bucket.

Ok... how? There's no information in their documentation for this. S3Cmd says it's a 'fully-featured' S3 command-line tool, but it makes no reference to versions other than its own. Is there any way to do this without using the web interface, which will take forever and requires me to keep my laptop on?


1 Answer

+2 votes
by (18.2k points)
selected by
Best answer

You can iterate through the version and delete them. This process can be a little bit tricky on AWS CLI but you can achieve this using the following Java code:

AmazonS3Client s3 = new AmazonS3Client();

String bucket_Name = "deleteversions-"+UUID.randomUUID();

//Create a Bucket


//Enable Versioning

BucketVersioningConfiguration configuration = new BucketVersioningConfiguration(ENABLED);

s3.setBucketVersioningConfiguration(new SetBucketVersioningConfigurationRequest(bucket_Name, configuration ));

//Put all the versions

s3.putObject(bucket_Name, "some-key",new ByteArrayInputStream("some-bytes".getBytes()), null);

s3.putObject(bucket_Name, "some-key",new ByteArrayInputStream("other-bytes".getBytes()), null);

//Remove all the versions

for ( S3VersionSummary version : S3Versions.inBucket(s3, bucket_Name) ) {

    String key = version.getKey();

    String versionId = version.getVersionId();         

    s3.deleteVersion(bucket_Name, key, versionId);


//Remove the bucket



Related questions

Want to get 50% Hike on your Salary?

Learn how we helped 50,000+ professionals like you !

0 votes
1 answer

Browse Categories