I would like to delete all files from an azure storage container where the filename includes test.zip
I am able to get all the files and export them to a file like this:
$context = New-AzureStorageContext -StorageAccountName ACCOUNTNAME `
-StorageAccountKey ACCOUNTKEY
get-azurestorageblob -Container CONTAINERNAME `
-blob *test.zip `
-Context $context | Out-File c:\files\f.txt `
-width 500
What is the simplest way to delete all the results using the remove-azurestorageblob
command?
Since blob storage only support individual blob deletes at this moment (i.e. it doesn't support batch deletes), your only option would be to read individual blobs from the output file and delete the blob one by one.
Based on your comments below, try the following:
$context = New-AzureStorageContext -StorageAccountName "account name" -StorageAccountKey "account key"
Get-AzureStorageBlob -Container "containername" -blob *test.zip -Context $context | Remove-AzureStorageBlob
The script above will first list matching blobs and then delete them one by one.
I had this challenge when setting up Azure storage accounts for Static website hosting using Powershell in Octopus Deploy.
Here's how I fixed it:
Using the Az module for Azure Powershell I did the following:
# Define Variables
$RESOURCE_GROUP_NAME = my-resource-group
$STORAGE_ACCOUNT_NAME = myapplication
# Get Storage Account Context
$STORAGE_ACCOUNT = Get-AzStorageAccount -ResourceGroupName $RESOURCE_GROUP_NAME -AccountName $STORAGE_ACCOUNT_NAME
$CONTEXT = $STORAGE_ACCOUNT.Context
# Remove all test.zip Files from the $web Storage Container
Get-AzStorageBlob -Container '$web' -Blob *test.zip -Context $CONTEXT | Remove-AzStorageBlob
OR
# Remove all Files and Folders from the $web Storage Container
Get-AzStorageBlob -Container '$web' -Blob * -Context $CONTEXT | Remove-AzStorageBlob
Write-Host 'Removed all files and folders from the $web Storage Container'
That's all.
I hope this helps
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With