AWS Java SDK S3 Delete Objects Examples
- Details
- Written by Nam Ha Minh
- Last Updated on 03 January 2022   |   Print Email
In this AWS Java SDK S3 tutorial series, I’d like to share with you a couple of code examples that delete objects in a bucket on Amazon S3 programmatically. In details, you will learn how to delete a single object per request and delete multiple objects per request.
Be sure that you have AWS SDK set up on your computer. If not, follow this guide: Setup AWS SDK for Java for S3. And note that the AWS credentials you’re using must have write permission on the objects which you want to delete.
1. Delete a Single Object per Request
The following Java code example shows how to delete an object identified by a given key, in a given bucket on Amazon S3 server:
package net.codejava.aws; import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.DeleteObjectRequest; public class DeleteSingleObjectExample { public static void main(String[] args) { String bucket = "your-bucket-name"; String key = "your-file-name"; S3Client client = S3Client.builder().build(); DeleteObjectRequest request = DeleteObjectRequest.builder() .bucket(bucket) .key(key) .build(); client.deleteObject(request); } }
Note that the S3Client.deleteObject() method sends a request for deleting a single object stored on S3, and it returns normally even in case the specified key does not exist.
2. Delete Multiple Objects per Request
The following Java code example illustrates how to delete multiple S3 objects in a single request:
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.DeleteObjectsRequest; import software.amazon.awssdk.services.s3.model.DeleteObjectsResponse; import software.amazon.awssdk.services.s3.model.ObjectIdentifier; public class DeleteMultipleObjectsExample { public static void main(String[] args) { String bucket = "your-bucket-name"; String key1 = "Key of object #1"; String key2 = "Key of object #2"; String key3 = "Key of object #3"; S3Client client = S3Client.builder().build(); List<ObjectIdentifier> listObjects = new ArrayList<>(); listObjects.add(ObjectIdentifier.builder().key(key1).build()); listObjects.add(ObjectIdentifier.builder().key(key2).build()); listObjects.add(ObjectIdentifier.builder().key(key3).build()); DeleteObjectsRequest request = DeleteObjectsRequest.builder() .bucket(bucket) .delete(Delete.builder().objects(listObjects).build()) .build(); DeleteObjectsResponse response = client.deleteObjects(request); System.out.println("Deleted: " + response.hasDeleted()); } }
And you can use the methods hasDeleted() and hasErrors() of the DeleteObjectsResponse class to check the result.
That’s a couple of examples about deleting S3 objects programmatically using AWS SDK for Java. Watch the video below to see the coding in action:
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 Buckets Examples