Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure: Delete all *test.zip blobs from Azure Storage using Powershell

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?

like image 357
Marcus Avatar asked May 31 '14 07:05

Marcus


2 Answers

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.

like image 81
Gaurav Mantri Avatar answered Oct 04 '22 00:10

Gaurav Mantri


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

like image 37
Promise Preston Avatar answered Oct 04 '22 01:10

Promise Preston