Delete empty blob directory

Viewed 32

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:

#1 enter image description here

#2 enter image description here

#3

enter image description here

#4 enter image description here

Any ideas how can improve my function?

1 Answers

Considering your Storage Account is ADLS Gen2, you cannot use Blob Storage SDK to delete folders as the folders in your General Purpose Storage accounts are virtual folders (they are simply blob prefixes).

In order to delete the folders, you would want to use SDK specific for ADLS Gen2 accounts. The SDK you would want to use is com.azure:azure-storage-file-datalake.

You can find more details about using the SDK and some code samples here: https://docs.microsoft.com/en-us/azure/storage/blobs/data-lake-storage-directory-file-acl-java.

Related