diff --git a/src/main/java/org/elasticsearch/cloud/aws/blobstore/S3BlobStore.java b/src/main/java/org/elasticsearch/cloud/aws/blobstore/S3BlobStore.java index 63e5bb647a9..c6af86cef87 100644 --- a/src/main/java/org/elasticsearch/cloud/aws/blobstore/S3BlobStore.java +++ b/src/main/java/org/elasticsearch/cloud/aws/blobstore/S3BlobStore.java @@ -98,18 +98,20 @@ public class S3BlobStore extends AbstractComponent implements BlobStore { @Override public void delete(BlobPath path) { ObjectListing prevListing = null; + //From http://docs.amazonwebservices.com/AmazonS3/latest/dev/DeletingMultipleObjectsUsingJava.html + //we can do at most 1K objects per delete + int objectCount = 0; + //We don't know the bucket name until first object listing + DeleteObjectsRequest multiObjectDeleteRequest = null; + ArrayList keys = new ArrayList(); while (true) { ObjectListing list; if (prevListing != null) { list = client.listNextBatchOfObjects(prevListing); } else { list = client.listObjects(bucket, path.buildAsString("/") + "/"); + multiObjectDeleteRequest = new DeleteObjectsRequest(list.getBucketName()); } - //From http://docs.amazonwebservices.com/AmazonS3/latest/dev/DeletingMultipleObjectsUsingJava.html - //we can do at most 1K objects per delete - int objectCount = 0; - DeleteObjectsRequest multiObjectDeleteRequest = new DeleteObjectsRequest(list.getBucketName()); - ArrayList keys = new ArrayList(); for (S3ObjectSummary summary : list.getObjectSummaries()) { objectCount++; keys.add(new KeyVersion(summary.getKey())); @@ -122,16 +124,16 @@ public class S3BlobStore extends AbstractComponent implements BlobStore { objectCount = 0; } } - if (objectCount > 0) { - multiObjectDeleteRequest.setKeys(keys); - client.deleteObjects(multiObjectDeleteRequest); - } if (list.isTruncated()) { prevListing = list; } else { break; } } + if (objectCount > 0) { + multiObjectDeleteRequest.setKeys(keys); + client.deleteObjects(multiObjectDeleteRequest); + } } @Override