amazon-web-servicesamazon-s3aws-cli

Delete Full S3 Bucket CLI


I need to delete an S3 bucket that has some objects in it:

aws s3 rb --force  s3://ansible.prod-us-east 
remove_bucket failed: s3://ansible.prod-us-east 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.

I also tried this:

aws s3api delete-bucket --bucket "ansible.prod-us-east" --region "us-east-1" 

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.

The errors say that the bucket is full. But when I list it on the command line or look at the bucket in the console the bucket is already empty.

I get the same error when I try to delete the bucket from the console. The bucket is empty but the error says that it's full and I can't delete the bucket.

How can I get this done?


Solution

  • The errors say that the bucket is full. But when I list it on the command line or look at the bucket in the console the bucket is already empty.

    It is a versioned bucket. In AWS Console, by default, Versions are hidden. You have an option to show the versioned objects and delete markers. To view the versions in CLI,

    aws s3api list-object-versions --bucket ansible.prod-us-east
    

    To delete the bucket, all the versions and the delete markers must be deleted first. It is easier to get this done in boto3 than aws-cli.

    #!/usr/bin/env python
    import boto3
    
    s3 = boto3.resource('s3')
    bucket = s3.Bucket('ansible.prod-us-east')
    bucket.object_versions.all().delete()
    bucket.delete()
    

    Try this, if you want to get this done with aws s3api.