Search code examples
azurepowershellazure-powershell

Powershell - remove all blobs in a container


I need to remove all of the blobs in a specific container before doing a backup to that container. The following does not work. What is the proper way to do this?

Get-AzStorageBlob -Container $ContainerName -Context $ctx |  Remove-AzStorageBlob -DeleteSnapshot -Force

I get the following error message for each blob returned by Get-AzStorageBlob:

Remove-AzStorageBlob : The specified blob does not exist. HTTP Status Code: 404 - HTTP Error Message: The specified blob does not exist.
ErrorCode: BlobNotFound
ErrorMessage: The specified blob does not exist.
RequestId:eb2612f4-f01e-0067-5471-3e9f69000000
Time:2019-07-19T20:33:50.1261168Z
At line:15 char:62
+ ... inerName -Context $ctx |  Remove-AzStorageBlob -DeleteSnapshot -Force
+                               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : CloseError: (:) [Remove-AzStorageBlob], StorageException
    + FullyQualifiedErrorId : StorageException,Microsoft.WindowsAzure.Commands.Storage.Blob.RemoveStorageAzureBlobCommand

Solution

  • I test with your ps script, it's caused by the -DeleteSnapshot -Force, delete it and then it works well.

    Get-AzureStorageBlob -Container $ContainerName -Context $ctx | Remove-AzureStorageBlob
    

    enter image description here

    And if you want to delete snapshot, just add -Force. Here is the Parameters description, -Force indicates that this cmdlet removes the blob and its snapshot without confirmation.

    You could have a try, hope this could help you.

    Note: If you are using the newer Az Module, the above code will look like:

    Get-AzStorageBlob -Container $ContainerName -Context $ctx | Remove-AzStorageBlob