0

I am working on a cleanup script that deletes an Azure image and its underlying storage blob. I can find the storage blob for my image with this command:

az image list --query "[?name=='$IMAGE_NAME'] | [].storageProfile.osDisk.blobUri"

(This is bash, so $IMAGE_NAME gets replaced with the actual image name). The output of the above command is a JSON list of URIs, each looking something like this:

https://storage_account.blob.core.windows.net/container_name/blob_name.vhd

Looking at the documentation for az storage blob delete, I can tell that this blob can be deleted with a command like this:

az storage blob delete --account-name storage_account --container container_name --name blob_name.vhd

So, obviously I can parse the URI and then generate this command. However, this seems odd: what's the point of giving blobs a URI if you can't use them?

So my question is:

  1. Is there a direct az cli command to delete a blob by using its URI?
  2. Better yet, is there a way to delete the blob associated with a given Azure image?
itsadok
  • 28,822
  • 30
  • 126
  • 171

1 Answers1

1

There is no built-in CLI command to delete with blob url directly. There is a workaround to use az rest to call the Delete Blob REST API.

access_token = $(az account get-access-token --resource https://storage.azure.com/ --query accessToken -o tsv)
now = $(env LANG=en_US TZ=GMT date '+%a, %d %b %Y %T %Z')
headers = "Authorization=Bearer "+$access_token+" x-ms-date="+$now+" x-ms-version=2020-08-04"
az rest --method delete --uri $blob_url --headers $headers 
unknown
  • 6,778
  • 1
  • 5
  • 14