How to delete azure blob (file) from the Storage account using PowerShell?

PowerShellSoftware & CodingMicrosoft Technologies

To delete the Azure blob from the azure storage account using PowerShell, we can use the RemoveAzStorageBlob command. Before running this command, we need to make sure that the Azure cloud account is connected (Connect-AzAccount) and the proper subscription is set in which the storage account resides (Set-AzContext).

Once the above two commands are executed, we can use the Remove-AzStorageBlob command but we need to use the storage context to work with the Azure Storage account.

The below commands will set the context for the Azure storage account with the storage account key.

$rg = "az204"
$storageaccount = "az204storage05june"
$key = (Get-AzStorageAccountKey -ResourceGroupName $rg - Name $storageaccount)[0].Value
$context = New-AzStorageContext -StorageAccountName $storageaccount - StorageAccountKey $key

The below will remove azure storage blob from the storage container ‘container1’.

Remove-AzStorageBlob -Container 'container1' -Context $context -Blob '' - Verbose

-Blob parameter accepts only one file or blob at a time. If you want to delete the entire content of the container then you need to use a loop.

foreach($file in (Get-AzStorageBlob -Container 'container1' -
Context $context).Name){
   Remove-AzStorageBlob -Container 'container1' -Context $context -Blob $file - Verbose
Published on 01-Sep-2021 08:03:32