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:


About the Author:

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.