Emptying a bucket
You can empty a bucket's contents using the Amazon S3 console, AWS SDKs, or AWS Command Line Interface (AWS CLI). When you empty a bucket, you delete all the objects, but you keep the bucket. After you empty a bucket, it cannot be undone. Objects added to the bucket while the empty bucket action is in progress might be deleted. All objects (including all object versions and delete markers) in the bucket must be deleted before the bucket itself can be deleted.
When you empty a bucket that has S3 Versioning enabled or suspended, all versions of all the objects in the bucket are deleted. For more information, see Working with objects in a versioning-enabled bucket.
You can also specify a lifecycle configuration on a bucket to expire objects so that Amazon S3
can delete them. For more information, see Setting an S3 Lifecycle configuration on a
bucket. To empty a large bucket, we recommend that you
use an S3 Lifecycle configuration rule. Lifecycle expiration is an asynchronous process, so the rule might take some days
to run before the bucket is empty. After the first time that Amazon S3 runs the rule, all objects that are eligible for expiration are marked for deletion. You're no longer charged for those objects that are marked for deletion. For more information, see How do I empty an Amazon S3 bucket using a lifecycle configuration rule?
You can use the Amazon S3 console to empty a bucket, which deletes all of the objects in the bucket without deleting the bucket.
To empty an S3 bucket
Sign in to the AWS Management Console and open the Amazon S3 console at https://console.aws.amazon.com/s3/
. -
In the Bucket name list, select the option next to the name of the bucket that you want to empty, and then choose Empty.
-
On the Empty bucket page, confirm that you want to empty the bucket by entering the bucket name into the text field, and then choose Empty.
-
Monitor the progress of the bucket emptying process on the Empty bucket: Status page.
You can empty a bucket using the AWS CLI only if the bucket does not have Bucket
Versioning enabled. If versioning is not enabled, you can use the rm
(remove) AWS CLI command with the --recursive
parameter to empty the
bucket (or remove a subset of objects with a specific key name prefix).
The following rm
command removes objects that have the key name
prefix doc
, for example, doc/doc1
and
doc/doc2
.
$
aws s3 rm s3://bucket-name/doc --recursive
Use the following command to remove all objects without specifying a prefix.
$
aws s3 rm s3://bucket-name --recursive
For more information, see Using high-level S3 commands with the AWS CLI in the AWS Command Line Interface User Guide.
Note
You can't remove objects from a bucket that has versioning enabled. Amazon S3 adds a delete marker when you delete an object, which is what this command does. For more information about S3 Bucket Versioning, see Retaining multiple versions of objects with S3 Versioning.
You can use the AWS SDKs to empty a bucket or remove a subset of objects that have a specific key name prefix.
For an example of how to empty a bucket using AWS SDK for Java, see Deleting a bucket. The code deletes all objects, regardless of whether the bucket has versioning enabled, and then it deletes the bucket. To just empty the bucket, make sure that you remove the statement that deletes the bucket.
For more information about using other AWS SDKs, see Tools for Amazon Web Services
To empty a large bucket, we recommend that you
use an S3 Lifecycle configuration rule. Lifecycle expiration is an asynchronous process, so the rule might take some days
to run before the bucket is empty. After the first time that Amazon S3 runs the rule, all objects that are eligible for expiration are marked for deletion. You're no longer charged for those objects that are marked for deletion. For more information, see How do I empty an Amazon S3 bucket using a lifecycle configuration rule?
If you use a lifecycle configuration to empty your bucket, the configuration should include current versions, non-current versions, delete markers, and incomplete multipart uploads.
You can add lifecycle configuration rules to expire all objects or a subset of objects that have a specific key name prefix. For example, to remove all objects in a bucket, you can set a lifecycle rule to expire objects one day after creation.
Amazon S3 supports a bucket lifecycle rule that you can use to stop multipart uploads that don't complete within a specified number of days after being initiated. We recommend that you configure this lifecycle rule to minimize your storage costs. For more information, see Configuring a bucket lifecycle configuration to delete incomplete multipart uploads.
For more information about using a lifecycle configuration to empty a bucket, see Setting an S3 Lifecycle configuration on a bucket and Expiring objects.
Emptying a bucket with AWS CloudTrail configured
AWS CloudTrail tracks object-level data events in an Amazon S3 bucket, such as deleting objects. If you use a bucket as a destination to log your CloudTrail events and are deleting objects from that same bucket you may be creating new objects while emptying your bucket. To prevent this, stop your AWS CloudTrail trails. For more information about stopping your CloudTrail trails from logging events, see Turning off logging for a trail in the AWS CloudTrail User Guide.
Another alternative to stopping CloudTrail trails from being added to the bucket is to add a
deny s3:PutObject
statement to your bucket policy. If you want to store new
objects in the bucket at a later time you will need to remove this deny
s3:PutObject
statement. For more information, see Object operations and IAM JSON policy
elements: Effect in the IAM User Guide.