delete-or-empty-bucket
Deleting or Emptying a Bucket
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, 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.
Topics
Delete a Bucket
You can delete a bucket and its content programmatically using the AWS SDKs. 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 these methods based on the number of objects in your bucket and the bucket's versioning status.
Topics
- Delete a Bucket: Using the Amazon S3 Console
- Delete a Bucket: Using the AWS CLI
- Delete a Bucket: Using the AWS SDKs
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.
For more information, see Using High-Level S3 Commands with the AWS Command Line Interface in the AWS Command Line Interface User Guide.
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 Java and .NET. 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 bucket that contains objects. The example deletes all objects, and then it deletes the bucket. The example works for buckets with or without versioning enabled.
Note
For buckets without versioning enabled, you can delete all objects directly and then delete the bucket. For buckets with versioning enabled, you must delete all object versions before deleting the bucket.
For instructions on creating and testing a working sample, see Testing the Amazon S3 Java Code Examples.
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.
Topics
- Empty a Bucket: Using the Amazon S3 console
- Empty a Bucket: Using the AWS CLI
- Empty a Bucket: Using Lifecycle Configuration
- Empty a Bucket: Using the AWS SDKs
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
.
Use the following command to remove all objects without specifying a prefix.
For more information, see Using High-Level S3 Commands with the AWS Command Line Interface in the AWS Command Line Interface User Guide.
Note
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.
Empty a Bucket: Using Lifecycle Configuration
You can configure lifecycle on your 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. To fully empty the contents of a versioning enabled bucket, you will need to configure an expiration policy on both current and noncurrent objects in the bucket.
For more information, see Object Lifecycle Management and Understanding Object Expiration.
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.
For more information about using other AWS SDKs, see Tools for Amazon Web Services.