Deleting or Emptying a Bucket

It is easy to delete an empty bucket, however in some situations you may need to delete
or
empty a bucket that contains objects. In this section, we'll explain how to delete
objects
in an unversioned bucket (the default), and how to delete object versions and delete
markers
in a bucket that has versioning enabled. For more information about versioning, see
Using Versioning. In some situations, you may choose to
empty a bucket instead of deleting it. This section explains various options you can
use to
delete or empty a bucket that contains objects.

Delete a Bucket

You can delete a bucket and its content programmatically using AWS SDK. You can also
use lifecycle configuration on a bucket to empty its content and then delete the bucket.
There are additional options, such as using Amazon S3 console and AWS CLI, but there
are
limitations on this method based on the number of objects in your bucket and the
bucket's versioning status.

Delete a Bucket: Using the Amazon S3 Console

The Amazon S3 console supports deleting a bucket that may or may not be empty.
For information about using the Amazon S3 console to delete a bucket, see How Do I Delete an S3 Bucket? in the
Amazon Simple Storage Service Console User Guide.

Delete a Bucket: Using the AWS CLI

You can delete a bucket that contains objects using the AWS CLI only if the bucket
does not have versioning enabled. If your bucket does not have versioning enabled,
you can use the rb (remove bucket) AWS CLI command with
--force parameter to remove a non-empty bucket. This command
deletes all objects first and then deletes the bucket.

To delete a non-empty bucket that does not have versioning enabled, you have the
following options:

Delete the bucket programmatically using the AWS SDK.

First, delete all of the objects using the bucket's lifecycle
configuration and then delete the empty bucket using the Amazon S3 console.

Delete a Bucket: Using Lifecycle
Configuration

You can configure lifecycle on your bucket to expire objects, Amazon S3 then deletes
expired objects. You can add lifecycle configuration rules to expire all or a subset
of objects with 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.

If your bucket has versioning enabled, you can also configure the rule to expire
noncurrent objects.

After Amazon S3 deletes all of the objects in your bucket, you can delete the bucket
or
keep it.

Important

If you just want to empty the bucket and not delete it, make sure you
remove the lifecycle configuration rule you added to empty the bucket so
that any new objects you create in the bucket will remain in the
bucket.

Delete a Bucket: Using the AWS SDKs

You can use the AWS SDKs to delete a bucket. The following sections provide
examples of how to delete a bucket using the AWS SDK for .NET and Java. First, the
code deletes objects in the bucket and then it deletes the bucket. For information
about other AWS SDKs, see Tools for Amazon Web
Services.

Delete a Bucket Using the
AWS SDK for Java

The following Java example deletes a non-empty bucket. First, the code deletes
all objects and then it deletes the bucket. The code example also works for
buckets with versioning enabled.

Empty a Bucket

You can empty a bucket's content (that is, delete all content, but keep the bucket)
programmatically using the AWS SDK. You can also specify lifecycle configuration on
a
bucket to expire objects so that Amazon S3 can delete them. There are additional options,
such as using Amazon S3 console and AWS CLI, but there are limitations on this method
based
on the number of objects in your bucket and the bucket's versioning status.

Empty a Bucket: Using the Amazon S3 console

For information about using the Amazon S3 console to empty a bucket, see How Do I Empty an S3 Bucket? in the
Amazon Simple Storage Service Console User Guide

Empty a Bucket: Using the AWS CLI

You can empty a bucket using the AWS CLI only if the bucket does not have
versioning enabled. If your bucket does not have versioning enabled, you can use the
rm (remove) AWS CLI command with the --recursive
parameter to empty a bucket (or remove a subset of objects with a specific key name
prefix).

The following rm command removes objects with key name prefix
doc, for example, doc/doc1 and
doc/doc2.

Copy

$ aws s3 rm s3://bucket-name/doc --recursive

Use the following command to remove all objects without specifying a
prefix.

You cannot remove objects from a bucket with versioning enabled. Amazon S3 adds
a delete marker when you delete an object, which is what this command will
do. For more information about versioning, see Using Versioning.

To empty a bucket with versioning enabled, you have the following options:

Delete the bucket programmatically using the AWS SDK.

Use the bucket's lifecycle configuration to request that Amazon S3 delete
the objects.

Use the Amazon S3 console (can only use this option if your bucket contains
less than 100,000 items—including both object versions and delete
markers).

Empty a Bucket: Using Lifecycle
Configuration

You can configure lifecycle on you bucket to expire objects and request that Amazon
S3
delete expired objects. You can add lifecycle configuration rules to expire all or
a
subset of objects with a specific key name prefix. For example, to remove all
objects in a bucket, you can set lifecycle rule to expire objects one day after
creation.

If your bucket has versioning enabled, you can also configure the rule to expire
noncurrent objects.

Warning

After your objects expire, Amazon S3 deletes the expired objects. If you just
want to empty the bucket and not delete it, make sure you remove the
lifecycle configuration rule you added to empty the bucket so that any new
objects you create in the bucket will remain in the bucket.

Empty a Bucket: Using the AWS SDKs

You can use the AWS SDKs to empty a bucket or remove a subset of objects with a
specific key name prefix.

For an example of how to empty a bucket using AWS SDK for Java, see Delete a Bucket Using the
AWS SDK for Java. The
code deletes all objects, regardless of whether the bucket has versioning enabled
or
not, and then it deletes the bucket. To just empty the bucket, make sure you remove
the statement that deletes the bucket.