I am encountering an issue when trying to delete all blobs under a specific prefix/directory in my Azure Blob Storage. When running the code below, item.isPrefix() returns null.
BlobContainerClient containerClient = blobServiceClient.getBlobContainerClient(containerName);
PagedIterable<BlobItem> blobItems = containerClient.listBlobsByHierarchy(folderKey);
Iterator<BlobItem> iterator = blobItems.iterator();
while (iterator.hasNext()) {
BlobItem item = iterator.next();
if (item.isPrefix() == false) {
BlockBlobClient blobClient = containerClient.getBlobClient(item.getName()).getBlockBlobClient();
blobClient.delete();
}
}
However, when debugging this code isPrefix actually has a value (false) set. This leads me to believe that the state of BloblItem is not finalised when running normally but when debugging the fact that the debugger halts execution allows for the BlobItem to be set fully. I read in the docs that listBlobsByHierarchy returns a "reactive publisher" but I am not sure how to handle that.
How can I safely delete all blobs under a specific directory?
NOTE: If I do not check for isPrefix and delete everything listBlobsByHierarchy returns I get a 404 error when trying to delete the BlobItem that represents a directory.
UPDATE:
Files hierarchy inside the container:
Container/
- directory1
- sub-directory1
- file1
- file2
- directory2
- sub-directory1
- file3