Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
awsdocs
GitHub Repository: awsdocs/amazon-s3-developer-guide
Path: blob/master/code_examples/java_examples/S3Examples/DeleteBucket.java
4084 views
1
// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
// SPDX-License-Identifier: MIT-0 (For details, see https://github.com/awsdocs/amazon-s3-developer-guide/blob/master/LICENSE-SAMPLECODE.)
3
4
import java.util.Iterator;
5
6
import com.amazonaws.AmazonServiceException;
7
import com.amazonaws.SdkClientException;
8
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
9
import com.amazonaws.services.s3.AmazonS3;
10
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
11
import com.amazonaws.services.s3.model.ListVersionsRequest;
12
import com.amazonaws.services.s3.model.ObjectListing;
13
import com.amazonaws.services.s3.model.S3ObjectSummary;
14
import com.amazonaws.services.s3.model.S3VersionSummary;
15
import com.amazonaws.services.s3.model.VersionListing;
16
17
public class DeleteBucket {
18
19
public static void main(String[] args) {
20
String clientRegion = "*** Client region ***";
21
String bucketName = "*** Bucket name ***";
22
23
try {
24
AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
25
.withCredentials(new ProfileCredentialsProvider())
26
.withRegion(clientRegion)
27
.build();
28
29
// Delete all objects from the bucket. This is sufficient
30
// for unversioned buckets. For versioned buckets, when you attempt to delete objects, Amazon S3 inserts
31
// delete markers for all objects, but doesn't delete the object versions.
32
// To delete objects from versioned buckets, delete all of the object versions before deleting
33
// the bucket (see below for an example).
34
ObjectListing objectListing = s3Client.listObjects(bucketName);
35
while (true) {
36
Iterator<S3ObjectSummary> objIter = objectListing.getObjectSummaries().iterator();
37
while (objIter.hasNext()) {
38
s3Client.deleteObject(bucketName, objIter.next().getKey());
39
}
40
41
// If the bucket contains many objects, the listObjects() call
42
// might not return all of the objects in the first listing. Check to
43
// see whether the listing was truncated. If so, retrieve the next page of objects
44
// and delete them.
45
if (objectListing.isTruncated()) {
46
objectListing = s3Client.listNextBatchOfObjects(objectListing);
47
} else {
48
break;
49
}
50
}
51
52
// Delete all object versions (required for versioned buckets).
53
VersionListing versionList = s3Client.listVersions(new ListVersionsRequest().withBucketName(bucketName));
54
while (true) {
55
Iterator<S3VersionSummary> versionIter = versionList.getVersionSummaries().iterator();
56
while (versionIter.hasNext()) {
57
S3VersionSummary vs = versionIter.next();
58
s3Client.deleteVersion(bucketName, vs.getKey(), vs.getVersionId());
59
}
60
61
if (versionList.isTruncated()) {
62
versionList = s3Client.listNextBatchOfVersions(versionList);
63
} else {
64
break;
65
}
66
}
67
68
// After all objects and object versions are deleted, delete the bucket.
69
s3Client.deleteBucket(bucketName);
70
}
71
catch(AmazonServiceException e) {
72
// The call was transmitted successfully, but Amazon S3 couldn't process
73
// it, so it returned an error response.
74
e.printStackTrace();
75
}
76
catch(SdkClientException e) {
77
// Amazon S3 couldn't be contacted for a response, or the client couldn't
78
// parse the response from Amazon S3.
79
e.printStackTrace();
80
}
81
}
82
}
83
84