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:
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:
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:
Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.