In this article, we explain how to fast delete all the objects in a given AWS S3 bucket. We will use batch deletes with the Java AWS SDK.
1) Java AWS SDK dependency
First, we need to add the Java AWS dependency to our pom.xml:
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-s3</artifactId>
<version>1.11.683</version>
</dependency>
2) Gather all the S3 objects keys in the bucket
Then, we gather all the S3 objects keys in the bucket. We stop when there is no more batch (or page) of objects in the bucket:
static Set<String> allBucketKeys(AmazonS3 s3Client, String bucket) {
Set<String> allKeys = new HashSet<>();
ObjectListing objectListing = s3Client.listObjects(bucket);
for (S3ObjectSummary sum : objectListing.getObjectSummaries())
allKeys.add(sum.getKey());
while (objectListing.isTruncated()) {
objectListing = s3Client.listNextBatchOfObjects(objectListing);
for (S3ObjectSummary sum : objectListing.getObjectSummaries())
allKeys.add(sum.getKey());
}
return allKeys;
}
3) Batch deletes
AWS S3 allows a maximum size of 1000 objects per batch delete (class DeleteObjectsRequest). So let’s iterate over the bucket keys from previous step, and delete batches of 1000 objects:
AmazonS3 s3Client = <your-s3-client>;
String bucket = "your-bucket-name";
List<KeyVersion> keyPageToDelete = new ArrayList<>();
for (String key : allBucketKeys(s3Client, bucket)) {
if (keyPageToDelete.size() >= 1000) {
deleteObjects(s3Client, bucket, keyPageToDelete);
keyPageToDelete = new ArrayList<>();
}
if (!key.startsWith(currentPrefix))
keyPageToDelete.add(new KeyVersion(key));
}
deleteObjects(s3Client, bucket, keyPageToDelete);
In the above code, you need to replace s3Client and bucket with your own values. Also, the function deleteObjects() is:
static void deleteObjects(AmazonS3 s3Client, String bucket, List<KeyVersion> keyPageToDelete) {
if (keyPageToDelete.isEmpty())
return;
DeleteObjectsRequest dor = new DeleteObjectsRequest(bucket);
dor.setKeys(keyPageToDelete);
s3Client.deleteObjects(dor);
}
That’s it for this tutorial ! If you have questions, please leave a reply below.