Path: blob/master/code_examples/java_examples/S3Examples/DeleteBucket.java
4084 views
// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.1// SPDX-License-Identifier: MIT-0 (For details, see https://github.com/awsdocs/amazon-s3-developer-guide/blob/master/LICENSE-SAMPLECODE.)23import java.util.Iterator;45import com.amazonaws.AmazonServiceException;6import com.amazonaws.SdkClientException;7import com.amazonaws.auth.profile.ProfileCredentialsProvider;8import com.amazonaws.services.s3.AmazonS3;9import com.amazonaws.services.s3.AmazonS3ClientBuilder;10import com.amazonaws.services.s3.model.ListVersionsRequest;11import com.amazonaws.services.s3.model.ObjectListing;12import com.amazonaws.services.s3.model.S3ObjectSummary;13import com.amazonaws.services.s3.model.S3VersionSummary;14import com.amazonaws.services.s3.model.VersionListing;1516public class DeleteBucket {1718public static void main(String[] args) {19String clientRegion = "*** Client region ***";20String bucketName = "*** Bucket name ***";2122try {23AmazonS3 s3Client = AmazonS3ClientBuilder.standard()24.withCredentials(new ProfileCredentialsProvider())25.withRegion(clientRegion)26.build();2728// Delete all objects from the bucket. This is sufficient29// for unversioned buckets. For versioned buckets, when you attempt to delete objects, Amazon S3 inserts30// delete markers for all objects, but doesn't delete the object versions.31// To delete objects from versioned buckets, delete all of the object versions before deleting32// the bucket (see below for an example).33ObjectListing objectListing = s3Client.listObjects(bucketName);34while (true) {35Iterator<S3ObjectSummary> objIter = objectListing.getObjectSummaries().iterator();36while (objIter.hasNext()) {37s3Client.deleteObject(bucketName, objIter.next().getKey());38}3940// If the bucket contains many objects, the listObjects() call41// might not return all of the objects in the first listing. Check to42// see whether the listing was truncated. If so, retrieve the next page of objects43// and delete them.44if (objectListing.isTruncated()) {45objectListing = s3Client.listNextBatchOfObjects(objectListing);46} else {47break;48}49}5051// Delete all object versions (required for versioned buckets).52VersionListing versionList = s3Client.listVersions(new ListVersionsRequest().withBucketName(bucketName));53while (true) {54Iterator<S3VersionSummary> versionIter = versionList.getVersionSummaries().iterator();55while (versionIter.hasNext()) {56S3VersionSummary vs = versionIter.next();57s3Client.deleteVersion(bucketName, vs.getKey(), vs.getVersionId());58}5960if (versionList.isTruncated()) {61versionList = s3Client.listNextBatchOfVersions(versionList);62} else {63break;64}65}6667// After all objects and object versions are deleted, delete the bucket.68s3Client.deleteBucket(bucketName);69}70catch(AmazonServiceException e) {71// The call was transmitted successfully, but Amazon S3 couldn't process72// it, so it returned an error response.73e.printStackTrace();74}75catch(SdkClientException e) {76// Amazon S3 couldn't be contacted for a response, or the client couldn't77// parse the response from Amazon S3.78e.printStackTrace();79}80}81}828384