How to Delete a Blob from an Azure Storage Account using PowerShell

Introduction 

In this blog, you will see how to delete a blob from an Azure Storage Account using PowerShell.

Prerequisites:

Install Azure PowerShell Module to run the script.

PowerShell Script:

Open Notepad and paste the following script. Save the file as script.ps1.

  1. ################# Azure Blob Storage - PowerShell ####################  
  2.   
  3. ## Input Parameters  
  4. $resourceGroupName="azpractice"  
  5. $storageAccName="azstorageacc1122020"  
  6. $containerName="container001"  
  7. $blobName="testdoc"  
  8.   
  9. ## Connect to Azure Account  
  10. Connect-AzAccount   
  11.   
  12. ## Function to delete a blob  
  13. Function DeleteBlob  
  14. {  
  15.     Write-Host -ForegroundColor Green "Deleting the blob.."      
  16.     ## Get the storage account in which container has to be created  
  17.     $storageAcc=Get-AzStorageAccount -ResourceGroupName $resourceGroupName -Name $storageAccName    
  18.     ## Get the storage account context  
  19.     $ctx=$storageAcc.Context  
  20.     ## Delete Blob  
  21.     Remove-AzStorageBlob -Container $containerName  -Context $ctx -Blob $blobName  
  22. }   
  23.   
  24. DeleteBlob   
  25.   
  26. ## Disconnect from Azure Account  
  27. Disconnect-AzAccount  
 Open a Windows PowerShell window and navigate to the location where the script file was saved.

Run the following command:

.\script.ps1

 

Reference:

https://docs.microsoft.com/en-us/powershell/module/az.storage/remove-azstorageblob?view=azps-3.3.0

Summary:

In this blog, you saw how to delete a blob from an Azure storage account using PowerShell.