Aws-cli: Unable to remove bucket in one step

Created on 14 Feb 2014  路  9Comments  路  Source: aws/aws-cli

[root@sgd ~]# aws --version   
aws-cli/1.2.13 Python/3.3.3 Linux/3.8.4-1-ARCH
[root@sgd ~]# aws s3 rb --recursive s3://resumetest
Unknown options: --recursive
[root@sgd ~]#  aws s3 rb s3://resumetest --recursive
Unknown options: --recursive
[root@sgd ~]#  aws s3 rb s3://resumetest            
remove_bucket failed: s3://resumetest/ A client error (BucketNotEmpty) occurred when calling the DeleteBucket operation: The bucket you tried to delete is not empty

What am I missing?

How do I remove a bucket in one step?

Most helpful comment

+1 for this - this is painful

All 9 comments

You can use the --force option:

       --force  Deletes all objects in the bucket including the bucket itself.

Although, we can improve the error message in this case to hint to the user what to do if the bucket is not empty.

I don't see this force option documented. aws s3 help rb | grep force

The format for help is aws s3 rb help

$ PAGER=cat aws s3 rb help
RB()                                                                      RB()



NAME
       rb -

DESCRIPTION
       Deletes an S3 bucket.

SYNOPSIS
            rb <S3Path>
          [--force]

OPTIONS
       --force  Deletes all objects in the bucket including the bucket itself.

EXAMPLES
       1) The following rb command removes a bucket.   In  this  example,  the
       user's bucket is mybucket.  Note that the bucket must be empty in order
       to remove.

          aws s3 rb s3://mybucket

       Output:

          remove_bucket: mybucket

       2) The following rb command uses the --force parameter to first  remove
       all of the objects in the bucket and then remove the bucket itself.  In
       this example, the user's bucket is mybucket and the objects in mybucket
       are test1.txt and test2.txt.

          aws s3 rb s3://mybucket --force

       Output:

          delete: s3://mybucket/test1.txt
          delete: s3://mybucket/test2.txt
          remove_bucket: mybucket

Wow aws s3 rb help is counter-intuitive if you work with ubiquitous tools like git. First I've seen of this type. getopts interfaces further broken.

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

How to remove the versions quickly

+1 for this - this is painful

:+1: Sept 2017?

August 2018

I am also running into this problem for a versioned bucket. There are some longer scripts people seemed to have posted for this as a resolution (either with boto or cli):

  1. https://stackoverflow.com/questions/29809105/how-do-i-delete-a-versioned-bucket-in-aws-s3-using-the-cli
    1b. (Same answer as #1 in comments): https://forums.aws.amazon.com/thread.jspa?threadID=167616
  2. Gist with cli that does something similar: https://gist.github.com/weavenet/f40b09847ac17dd99d16

These solutions seem very verbose for something that should be pretty simple and in the CLI

Was this page helpful?
0 / 5 - 0 ratings