I want to delete some blob directory using Azure Java SDK (https://docs.microsoft.com/en-us/java/api/com.microsoft.azure.storage.blob.cloudblobdirectory?view=azure-java-legacy). Storage account is ADLS Gen2.
I know the concept that to delete directory I need to delete all blobs "inside" it but still - even if I delete all blobs, in Azure Storage Explorer I can see empty directories, and I want to get rid of them also.
I have tried such code and it works great but I'm stuck when it comes to delete empty blob directory:
val storageAccountName = "<account-name>"
val containerName = "<container-name>"
val sas = "<sas>"
val path = "copyTest/testDirectory"
val container: CloudBlobContainer = CloudStorageAccount
.parse(s"DefaultEndpointsProtocol=https;AccountName=${storageAccountName};SharedAccessSignature=${sas};EndpointSuffix=core.windows.net")
.createCloudBlobClient().getContainerReference(containerName)
def deleteBlobs(blobs: Iterable[ListBlobItem]): Unit = {
for (blob <- blobs) {
blob match {
case blockBlob: CloudBlockBlob => {
logger.info("blob")
logger.info(blockBlob.getUri.getPath)
blockBlob.delete()
}
case blobDirectory: CloudBlobDirectory => {
if (blobDirectory.listBlobs.iterator().hasNext) {
logger.info("blobDirectory not empty")
logger.info(blobDirectory.getUri.getPath)
val blobsFromDir = container.listBlobs(blobDirectory.getUri.getPath.replace(containerName + "/", ""))
import scala.jdk.CollectionConverters._
deleteBlobs(blobsFromDir.asScala)
} else {
logger.info("blobDirectoryEmpty")
logger.info(blobDirectory.getUri.getPath)
// blobDirectory.delete() #1
// blob.delete() #2
// val targetDirectory = container.getBlockBlobReference(
blobDirectory.getUri.getPath.replaceFirst(destContainerName + "/", "").substring(1)) #3
// val targetDirectory = destContainer.getDirectoryReference(
blobDirectory.getUri.getPath.replaceFirst(destContainerName + "/", "").substring(1)) #4
// targetDirectory.delete()
}
}
case _ => logger.info(s"Unknown blob type")
}
}
}
import scala.jdk.CollectionConverters._
val blobs = container.listBlobs(path).asScala
deleteBlobs(blobs)
I get response:
#3
Any ideas how can improve my function?



