s3:DeleteBucket returns BucketNotEmpty.
This guide walks through emptying a bucket of every kind of content it can hold, then deleting the empty bucket. The four cleanup steps are independent and can run in parallel: skip any that do not apply to your bucket.
Prerequisites
Before you start:- An Access Key for the organization that owns the bucket.
- A configured S3-compatible client (AWS CLI, s3cmd, or Boto3).
- Permission to call
s3:DeleteObject,s3:DeleteObjects,s3:AbortMultipartUpload, ands3:DeleteBucketon the bucket.
Step 1: Delete current objects
Delete all current-version objects in the bucket.- AWS CLI
- s3cmd
- Boto3
aws s3 rm with --recursive removes every current object in the bucket. Replace [BUCKET-NAME] with the name of your bucket.Delete all current objects with AWS CLI
Bulk deletion performance
For large buckets, choose the deletion approach that matches the scale you need:s3:DeleteObjectsbatches delete up to 1000 objects per call. The Boto3 example above uses this approach. It is the most efficient way to delete millions of objects from a single client.- Parallel
aws s3 rmworkers: Split deletion across prefixes and run multipleaws s3 rm s3://[BUCKET-NAME]/[PREFIX] --recursivecommands in parallel. Reasonable for hundreds of thousands of objects when the keys partition cleanly by prefix. rclone delete: Runrclone delete [REMOTE]:[BUCKET-NAME]for incremental deletion with progress reporting. Combine with--transfersto control concurrency. See Versioned buckets for rclone configuration.
Expiration lifecycle rule deletes objects on a schedule without consuming client capacity.
Step 2: Delete noncurrent versions and delete markers
Skip this step if the bucket does not have versioning enabled or suspended. To check:Check the versioning status of a bucket
"Status": "Enabled" or "Status": "Suspended", the bucket can hold noncurrent versions and delete markers, both of which must be removed before deletion.
The simplest way to remove every noncurrent version and delete marker is to list them and delete them by VersionId.
- AWS CLI
- s3cmd
- Boto3
This shell loop pages through If the bucket has more than 1000 versions or delete markers, repeat both blocks until both listings return no entries.
list-object-versions and deletes every version and delete marker it finds. Replace [BUCKET-NAME] with the name of your bucket.Delete all versions and delete markers with AWS CLI
Step 3: Abort incomplete multipart uploads
Incomplete multipart uploads do not appear in normal object listings, but they consume storage and prevent bucket deletion. They are the single most common reasonDeleteBucket returns BucketNotEmpty when the bucket appears empty in the Cloud Console or in aws s3 ls.
To check whether the bucket has incomplete multipart uploads:
List incomplete multipart uploads
Step 4: Delete the empty bucket
Once the bucket has no current objects, no noncurrent versions, no delete markers, and no incomplete multipart uploads, delete it.- AWS CLI
- s3cmd
- Boto3
Replace
[BUCKET-NAME] with the name of your bucket.Delete the empty bucket with AWS CLI
BucketNotEmpty, re-run the listings from each step above to find what remains.
Prevent BucketNotEmpty errors
To keep buckets ready for deletion at any time, apply a bucket lifecycle policy that includes:- An
AbortIncompleteMultipartUploadrule. This is the most common cause of bucket-delete failures and the only way to keep abandoned uploads from accumulating without active intervention. - A
NoncurrentVersionExpirationrule on versioned buckets. Without it, every overwrite leaves a noncurrent version that must be cleaned up before deletion.
Related
- Bucket lifecycle policies: Automate the cleanup of versions and incomplete multipart uploads.
- Abort incomplete multipart uploads: Detailed procedures for listing and aborting MPUs.
- Versioned buckets: Manage versioned bucket workflows with rclone and the AWS CLI.
- Manage buckets: Bucket creation and management with S3-compatible clients.