AWS Java SDK S3 Delete Buckets Examples
- Details
- Written by Nam Ha Minh
- Last Updated on 27 December 2021   |   Print Email
In this AWS Java SDK S3 article, you will learn how to write Java code for deleting buckets on Amazon S3 server programmatically. In details, I’ll show code examples about:
- Delete an empty bucket on S3
- Delete a non-empty bucket on S3
Be sure that you already set up AWS SDK on your computer. If not, refer this article: Setup AWS SDK for Java for S3. And note that the AWS credentials you’re using must have WRITE permission to be able to delete S3 buckets.
1. Delete an Empty Bucket on S3
The following code example shows how to delete an empty bucket on Amazon S3 server programmatically using AWS SDK for Java:
package net.codejava.aws; import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.DeleteBucketRequest; public class DeleteEmptyBucketExample { public static void main(String[] args) { String bucketName = "your-bucket-name"; S3Client client = S3Client.builder().build(); DeleteBucketRequest request = DeleteBucketRequest.builder() .bucket(bucketName).build(); client.deleteBucket(request); System.out.println("Bucket " + bucketName + " was deleted."); } }
If the bucket does not exist, you would get the following error:
software.amazon.awssdk.services.s3.model.NoSuchBucketException: The specified bucket does not exist
Or if the bucket is not empty (containing objects), you would get the following error:
software.amazon.awssdk.services.s3.model.S3Exception: The bucket you tried to delete is not empty
So consider handling NoSuchBucketException and S3Exception when deleting a bucket.
2. Delete a Non-empty Bucket on S3
To delete a non-empty bucket on S3, firstly you have to delete all objects within the bucket, and then delete the bucket itself. The following code example illustrates deleting a non-empty S3 bucket:
package net.codejava.aws; import java.util.ArrayList; import java.util.List; import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.Delete; import software.amazon.awssdk.services.s3.model.DeleteBucketRequest; import software.amazon.awssdk.services.s3.model.DeleteObjectsRequest; import software.amazon.awssdk.services.s3.model.DeleteObjectsResponse; import software.amazon.awssdk.services.s3.model.ListObjectsRequest; import software.amazon.awssdk.services.s3.model.ListObjectsResponse; import software.amazon.awssdk.services.s3.model.ObjectIdentifier; import software.amazon.awssdk.services.s3.model.S3Object; public class DeleteNonEmptyBucketExample { public static void main(String[] args) { String bucketName = "your-bucket-name"; S3Client client = S3Client.builder().build(); ListObjectsRequest listRequest = ListObjectsRequest.builder() .bucket(bucketName).build(); ListObjectsResponse listResponse = client.listObjects(listRequest); List<S3Object> listObjects = listResponse.contents(); List<ObjectIdentifier> objectsToDelete = new ArrayList<ObjectIdentifier>(); for (S3Object s3Object : listObjects) { objectsToDelete.add(ObjectIdentifier.builder().key(s3Object.key()).build()); } DeleteObjectsRequest deleteObjectsRequest = DeleteObjectsRequest.builder() .bucket(bucketName) .delete(Delete.builder().objects(objectsToDelete).build()) .build(); DeleteObjectsResponse deleteObjectsResponse = client.deleteObjects(deleteObjectsRequest); System.out.println("Objects deleted: " + deleteObjectsResponse.hasDeleted()); DeleteBucketRequest request = DeleteBucketRequest.builder().bucket(bucketName).build(); client.deleteBucket(request); System.out.println("Bucket " + bucketName + " deleted."); } }
As you can see, this program sends 3 requests to Amazon S3 server in sequence:
- the first request for listing all objects in the specified bucket
- the second request for deleting all objects in the bucket
- the last request for deleting the bucket itself (it is empty after all objects deleted)
That’s a couple of Java code examples for deleting buckets on S3. I hope you found this article helpful.
Related AWS Java SDK Tutorials:
- How to Generate AWS Access Key ID and Secret Access Key
- How to setup AWS SDK for Java for Amazon S3 Development
- AWS Java SDK S3 List Buckets Example
- AWS Java SDK S3 List Objects Examples
- AWS Java SDK S3 Create Bucket Examples
- AWS Java SDK S3 Create Folder Examples
- Upload File to S3 using AWS Jav SDK - Java Console Program
- Upload File to S3 using AWS Java SDK - Java Servlet JSP Web App
- Spring Boot File Upload to Amazon S3 Example
- AWS Java SDK Download File from S3 Example
- AWS Java SDK S3 Delete Objects Examples