To empty an S3 bucket using the AWS CLI, you have to run the command aws s3api delete-objects
in your terminal. After the bucket has been emptied, you can then proceed to delete it.
Note: if an S3 bucket still contains objects, then you can’t delete the bucket.
When using an AWS CLI command to empty an S3 bucket on a specified account, make sure to log in to the specified AWS CLI profile. Otherwise, you can’t run any AWS CLI commands on your AWS account.
Here are the steps for how to empty and delete an S3 bucket using the AWS CLI.
Table of Contents
How to empty an S3 bucket using AWS CLI
First, you have to determine whether the S3 bucket you want to empty has versioning enabled. The following command checks if the S3 bucket versioning is enabled:
aws s3api get-bucket-versioning --bucket my-bucket
If the S3 bucket is versioned it will return the following output:
{
"Status": "Enabled"
}
Please run the command in step 2. Empty the versioned S3 bucket. If it’s not versioned, then you can run the command in step 1. Empty the S3 bucket.
1. Empty an S3 bucket
The following command will delete all objects in an S3 bucket with versioning disabled.
aws s3 rm s3://mybucket --recursive
This will recursively delete all objects in the S3 bucket, as you can see in the output below:
delete: s3://my-bucket/buildspec_release.yml
delete: s3://my-bucket/buildspec_test.yml
delete: s3://my-bucket/requirements.txt
delete: s3://my-bucket/pyproject.toml
delete: s3://my-bucket/docs/img/architecture.png.example
delete: s3://my-bucket/scripts/add_cloudformation_autodoc.py
delete: s3://my-bucket/docs/readme.md.example
delete: s3://my-bucket/scripts/cleanup_tests.sh
delete: s3://my-bucket/scripts/copy_test_outputs_to_s3
delete: s3://my-bucket/scripts/get_release_version
delete: s3://my-bucket/scripts/package_sam
delete: s3://my-bucket/scripts/post_cleanup
delete: s3://my-bucket/scripts/pre_cleanup
delete: s3://my-bucket/scripts/release_to_template_store
delete: s3://my-bucket/scripts/set_execution_environment
delete: s3://my-bucket/src/requirements.txt
delete: s3://my-bucket/scripts/taskcat_run_tests.py
delete: s3://my-bucket/src/app.py
delete: s3://my-bucket/template_eu-central-1.yml
delete: s3://my-bucket/template_quickstarts/custom_resource/hello_world/app.py
delete: s3://my-bucket/template_eu-west-1.yml
delete: s3://my-bucket/template_quickstarts/custom_resource/hello_world/requirements.txt
delete: s3://my-bucket/template_quickstarts/custom_resource/template.yml
delete: s3://my-bucket/template_quickstarts/lambda/hello_world/app.py
delete: s3://my-bucket/template_quickstarts/lambda/hello_world/requirements.txt
delete: s3://my-bucket/template_quickstarts/lambda/template.yml
delete: s3://my-bucket/template_quickstarts/nested_stacks/route53_template.yml
delete: s3://my-bucket/template_quickstarts/nested_stacks/ec2_template.yml
delete: s3://my-bucket/template_quickstarts/nested_stacks/template.yml
delete: s3://my-bucket/templates/template.yml
2. Empty a versioned S3 bucket
To delete all versioned objects in an S3 bucket with versioning enabled, you have to run the aws s3api delete-objects
AWS CLI command with additional parameters:
aws s3api delete-objects --bucket my-bucket \
--delete "$(aws s3api list-object-versions \
--bucket "my-bucket" \
--output=json \
--query='{Objects: Versions[].{Key:Key,VersionId:VersionId}}')"
The –delete parameter accepts a list of keys with the name of the object and VersionId
specifies the version of the object to delete.
To dynamically fetch the Key
and VersionId
we create a shell variable in which we run the command $(aws s3api list-object-version --bucket "my-bucket" --output=json --query='{Objects: Versions[].{Key:Key,VersionId:VersionId}}')
Note: make sure to replace my-bucket
with your bucket name.
The following output will be displayed when the versioned objects get deleted from the S3 bucket:
{
"Deleted": [
{
"Key": "dssss37678987/buildspec_release.yml",
"VersionId": "IQoEUgk.OeYa7uxKo73OVnD.WP6BVBoq"
},
{
"Key": "dssss37678987/template_eu-west-1.yml",
"VersionId": "WsWfosfzuYjOTrMlMdshJh8CDfmDGzvH"
},
{
"Key": "dssss37678987/buildspec_test.yml",
"VersionId": "JJTgwE45RKq94eGsGSiXNmAgDY.U5hId"
},
{
"Key": "dssss37678987/template_eu-central-1.yml",
"VersionId": "VIEkJfnLQ5cXiHEWt.mbKNf9yj0QqqVq"
},
{
"Key": "dssss37678987/template_quickstarts/nested_stacks/route53_template.yml",
"VersionId": "null"
}
]
}
Delete an S3 bucket
Once all the objects have been deleted and the S3 bucket is empty, then it’s possible to delete the S3 bucket using the command:
aws s3 rb s3://my-bucket
Make sure the bucket is empty, otherwise, you’ll get the following error:
remove_bucket failed: s3://my-bucket An error occurred (BucketNotEmpty) when calling the DeleteBucket operation: The bucket you tried to delete is not empty
Or if you have an S3 bucket with versioning enabled. Then you might run in the following error when you try to delete the versioned S3 bucket:
the bucket you tried to delete is not empty. you must delete all versions in the bucket.
To fix the error, make sure to run the command: aws s3api delete-objects
to delete the versioned objects in the S3 bucket. The full command is listed in the previous step: Empty a versioned S3 bucket
Alternative option: Use this Python script to find, empty, and delete an S3 bucket
I’ve also written a python boto3 script that empties all (versioned)objects in an S3 bucket and then proceeds to delete it afterward.
The script can be found in my AWS Toolbox repository.
Conclusion
In order to empty an S3 bucket with the AWS CLI, you need to find out if S3 bucket versioning is enabled or not.
If the versioning is disabled, you can run the aws s3 rm
CLI command to delete all objects in the S3 bucket.
If versioning is enabled, you run the CLI command aws s3api delete-objects
to delete all versioned objects in the S3 bucket.
Once the S3 bucket is empty you can then proceed to delete it.