Deleting a bucket - Amazon Simple Storage Service
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Deleting a bucket

You can delete an empty Amazon S3 bucket. For information about emptying a bucket, see Emptying a bucket.

You can delete a bucket by using the Amazon S3 console, the Amazon Command Line Interface (Amazon CLI), the Amazon SDKs, or the Amazon S3 REST API.

Important

Before deleting a bucket, consider the following:

  • Bucket names are unique. If you delete a bucket, another Amazon user can use the name. If you want to continue to use the same bucket name, don't delete the bucket. We recommend that you empty the bucket and keep it.

  • If the bucket hosts a static website, and you created and configured an Amazon Route 53 hosted zone as described in Tutorial: Configuring a static website using a custom domain registered with Route 53, you must clean up the Route 53 hosted zone settings that are related to the bucket. For more information, see Step 2: Delete the Route 53 hosted zone.

  • If the bucket receives log data from Elastic Load Balancing (ELB), we recommend that you stop the delivery of ELB logs to the bucket before deleting it. After you delete the bucket, if another user creates a bucket using the same name, your log data could potentially be delivered to that bucket. For information about ELB access logs, see Access logs for your Classic Load Balancer in the User Guide for Classic Load Balancers and Access logs for your Application Load Balancer in the User Guide for Application Load Balancers.

Troubleshooting

If you are unable to delete an Amazon S3 bucket, consider the following:

  • Make sure that the bucket is empty – You can delete buckets only if they don't have any objects in them. Make sure that the bucket is empty. For information about emptying a bucket, see Emptying a bucket.

  • Make sure that there aren't any access points attached – You can delete buckets only if they don't have any S3 Access Points or Multi-Region Access Points attached within the same account. Before deleting the bucket, delete any same-account access points that are attached to the bucket.

  • Amazon Organizations service control policies (SCPs) and resource control policies (RCPs) – SCPs and RCPs can deny the delete permission on a bucket. For more information, see service control policies and resource control policies in the Amazon Organizations User Guide.

  • s3:DeleteBucket permissions – If you cannot delete a bucket, work with your IAM administrator to confirm that you have s3:DeleteBucket permissions. For information about how to view or update IAM permissions, see Changing permissions for an IAM user in the IAM User Guide. For troubleshooting information, see Troubleshoot access denied (403 Forbidden) errors in Amazon S3.

  • s3:DeleteBucket Deny statement – If you have s3:DeleteBucket permissions in your IAM policy and you can't delete a bucket, the bucket policy might include a Deny statement for s3:DeleteBucket. Buckets created by Amazon Elastic Beanstalk have a policy containing this statement by default. Before you can delete the bucket, you must delete this statement or the bucket policy.

Prerequisites

Before you can delete a bucket, you must empty it. For information about emptying a bucket, see Emptying a bucket.

To delete an S3 bucket
  1. Sign in to the Amazon Web Services Management Console and open the Amazon S3 console at https://console.amazonaws.cn/s3/.

  2. In the left navigation pane, choose General purpose buckets.

  3. In the General purpose buckets list, select the option button next to the name of the bucket that you want to delete, and then choose Delete at the top of the page.

  4. On the Delete bucket page, confirm that you want to delete the bucket by entering the bucket name in the text field, and then choose Delete bucket.

    Note

    If the bucket contains any objects, empty the bucket before deleting it by choosing the Empty bucket button in the This bucket is not empty error alert and following the instructions on the Empty bucket page. Then return to the Delete bucket page and delete the bucket.

  5. To verify that you've deleted the bucket, open the General purpose buckets list and enter the name of the bucket that you deleted. If the bucket can't be found, your deletion was successful.

The following example shows how to delete a bucket by using the Amazon SDK for Java. The code first deletes all objects in the bucket, and then it deletes the bucket. For information about using other Amazon SDKs, see Tools for Amazon Web Services.

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 Getting Started in the Amazon SDK for Java Developer Guide.

import com.amazonaws.AmazonServiceException; import com.amazonaws.SdkClientException; import com.amazonaws.auth.profile.ProfileCredentialsProvider; import com.amazonaws.regions.Regions; import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.AmazonS3ClientBuilder; import com.amazonaws.services.s3.model.*; import java.util.Iterator; public class DeleteBucket2 { public static void main(String[] args) { Regions clientRegion = Regions.DEFAULT_REGION; String bucketName = "*** Bucket name ***"; try { AmazonS3 s3Client = AmazonS3ClientBuilder.standard() .withCredentials(new ProfileCredentialsProvider()) .withRegion(clientRegion) .build(); // Delete all objects from the bucket. This is sufficient // for unversioned buckets. For versioned buckets, when you attempt to delete // objects, Amazon S3 inserts // delete markers for all objects, but doesn't delete the object versions. // To delete objects from versioned buckets, delete all of the object versions // before deleting // the bucket (see below for an example). ObjectListing objectListing = s3Client.listObjects(bucketName); while (true) { Iterator<S3ObjectSummary> objIter = objectListing.getObjectSummaries().iterator(); while (objIter.hasNext()) { s3Client.deleteObject(bucketName, objIter.next().getKey()); } // If the bucket contains many objects, the listObjects() call // might not return all of the objects in the first listing. Check to // see whether the listing was truncated. If so, retrieve the next page of // objects // and delete them. if (objectListing.isTruncated()) { objectListing = s3Client.listNextBatchOfObjects(objectListing); } else { break; } } // Delete all object versions (required for versioned buckets). VersionListing versionList = s3Client.listVersions(new ListVersionsRequest().withBucketName(bucketName)); while (true) { Iterator<S3VersionSummary> versionIter = versionList.getVersionSummaries().iterator(); while (versionIter.hasNext()) { S3VersionSummary vs = versionIter.next(); s3Client.deleteVersion(bucketName, vs.getKey(), vs.getVersionId()); } if (versionList.isTruncated()) { versionList = s3Client.listNextBatchOfVersions(versionList); } else { break; } } // After all objects and object versions are deleted, delete the bucket. s3Client.deleteBucket(bucketName); } catch (AmazonServiceException e) { // The call was transmitted successfully, but Amazon S3 couldn't process // it, so it returned an error response. e.printStackTrace(); } catch (SdkClientException e) { // Amazon S3 couldn't be contacted for a response, or the client couldn't // parse the response from Amazon S3. e.printStackTrace(); } } }

You can delete a bucket that contains objects with the Amazon CLI if the bucket doesn't have versioning enabled. When you delete a bucket that contains objects, all the objects in the bucket are permanently deleted, including objects that have been transitioned to the S3 Glacier Flexible Retrieval storage class.

If your bucket doesn't have versioning enabled, you can use the rb (remove bucket) Amazon CLI command with the --force parameter to delete the bucket and all the objects in it. This command deletes all objects first and then deletes the bucket.

If versioning is enabled, using the rb command with the --force parameter doesn't delete versioned objects, so the bucket deletion fails because the bucket isn't empty. For more information about deleting versioned objects, see Deleting object versions.

To use the following command, replace amzn-s3-demo-bucket with the name of the bucket that you want to delete:

$ aws s3 rb s3://amzn-s3-demo-bucket --force

For more information, see Using High-Level S3 Commands with the Amazon Command Line Interface in the Amazon Command Line Interface User Guide.