question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[feature request] Delete non-empty bucket

See original GitHub issue

When I try to delete a bucket, I get the following error message:

botocore.exceptions.ClientError: An error occurred (BucketNotEmpty) when calling the DeleteBucket operation: The bucket you tried to delete is not empty

I know there are objects in the bucket. When I tell boto to delete a bucket, I expect the objects to be deleted too.

I understand there are circumstances where users might like this warning. So what I propose is an option for the client.delete_bucket() function, like:

client.delete_bucket(Bucket='mybucketname',DeleteIfNonEmpty=True)

Where DeleteIfNonEmpty is False by default.

Then if DeleteIfNonEmpty is True, boto seamlessly deletes the whole bucket, just like deleting a directory in a file browser.

The alternative is for the user to

  • manually list every object in the bucket;
  • manually delete every object in the bucket; then
  • actually delete the bucket

This is a lot of work for something which takes 2 clicks in the web console. Boto should handle this work for me.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
joguSDcommented, Jul 25, 2017

Yes, if you want to ultimately use the functionality in s3tranfser we recommend using the interface exposed through boto as those will remain stable, where as the underlying s3transfer interfaces may not. However, higher level s3 logic could theoretically be added there.

We try not to add too much high level logic on top of API calls as that creates feature disparity between this SDK and the ones for other languages. To be blunt, a proper implementation of it would be more complex than the snippet above and realistically I don’t see this feature being incorporated in at the SDK level. This truly is a request for the service team.

For reference, a quick way to delete all (non-versioned) objects in a bucket using resource models:

import boto3

bucket = boto3.resource('s3').Bucket('bucketname')
bucket.objects.all().delete()
bucket.delete()

There is also a versioned objects collector that would allow you to do something similar for versioned objects, but do be careful as that may end up making a lot of requests.

2reactions
matt-telstracommented, Jul 20, 2017

Here’s the code I’ve written as a workaround. I propose that we just integrate this into boto. If someone can me point to the right location in the repo, I’ll make a pull request.

def delete_bucket_completely(bucket_name):

    client = boto3.client('s3')

    response = client.list_objects_v2(
        Bucket=bucket_name,
    )

    while response['KeyCount'] > 0:
        print('Deleting %d objects from bucket %s' % (len(response['Contents']),bucket_name))
        response = client.delete_objects(
            Bucket=bucket_name,
            Delete={
                'Objects':[{'Key':obj['Key']} for obj in response['Contents']]
            }
        )
        response = client.list_objects_v2(
            Bucket=bucket_name,
        )

    print('Now deleting bucket %s' % bucket_name)
    response = client.delete_bucket(
        Bucket=bucket_name
    )
Read more comments on GitHub >

github_iconTop Results From Across the Web

ECS: Can a not empty bucket be deleted? | Dell US
Customer is trying to delete a not empty bucket and is getting the error message Error 1013 (http: 400): Bad request body.
Read more >
Deleting a bucket - Amazon Simple Storage Service
Make sure the bucket is empty – You can only delete buckets that don't have any objects in them. Make sure the bucket...
Read more >
How to force delete a non empty S3 bucket with versioning ...
In the Buckets list, select the option next to the name of the bucket that you want to delete, and then choose Delete...
Read more >
Troubleshoot failed request to delete an Amazon S3 bucket
1. Open the Amazon S3 console. · 2. Open the bucket that you want to delete by choosing the bucket's name. · 3....
Read more >
delete-objects — AWS CLI 2.9.8 Command Reference
This action enables you to delete multiple objects from a bucket using a single HTTP request. If you know the object keys that...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found