amazon-web-servicesamazon-s3command-line-interfaceaws-clis3cmd

How do I delete a versioned bucket in AWS S3 using the CLI?


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?


Solution

  • One way to do it is iterate through the versions and delete them. A bit tricky on the CLI, but as you mentioned Java, that would be more straightforward:

    AmazonS3Client s3 = new AmazonS3Client();
    String bucketName = "deleteversions-"+UUID.randomUUID();
    
    //Creates Bucket
    s3.createBucket(bucketName);
    
    //Enable Versioning
    BucketVersioningConfiguration configuration = new BucketVersioningConfiguration(ENABLED);
    s3.setBucketVersioningConfiguration(new SetBucketVersioningConfigurationRequest(bucketName, configuration ));
    
    //Puts versions
    s3.putObject(bucketName, "some-key",new ByteArrayInputStream("some-bytes".getBytes()), null);
    s3.putObject(bucketName, "some-key",new ByteArrayInputStream("other-bytes".getBytes()), null);
    
    //Removes all versions
    for ( S3VersionSummary version : S3Versions.inBucket(s3, bucketName) ) {
        String key = version.getKey();
        String versionId = version.getVersionId();          
        s3.deleteVersion(bucketName, key, versionId);
    }
    
    //Removes the bucket
    s3.deleteBucket(bucketName);
    System.out.println("Done!");
    

    You can also batch delete calls for efficiency if needed.