AWS Code Sample
Catalog demonstrates how to empty and then delete an S3 bucket.

/** * Copyright 2018-2019, Inc. or its affiliates. All Rights Reserved. * * This file is licensed under the Apache License, Version 2.0 (the "License"). * You may not use this file except in compliance with the License. A copy of * the License is located at * * * * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ import java.util.Iterator; import com.amazonaws.AmazonServiceException; import com.amazonaws.SdkClientException; import com.amazonaws.auth.profile.ProfileCredentialsProvider; import; import; import; import; import; import; import; public class DeleteBucket { public static void main(String[] args) { String clientRegion = "*** Client 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,; } // 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 =; 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(); } } }

Sample Details

Service: s3

Last tested: 2019-01-28

Author: AWS

Type: full-example

On this page: