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

3

8

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?

Zest answered 31/5, 2014 at 7:33 Comment(0)
K
12

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.

Kee answered 31/5, 2014 at 7:37 Comment(5)
Thanks Guarav! Any chance your powershell skills extend to being able to help me write a script to read and delete using a foreach loop?Zest
That's what I'm trying to figure out :). Will update my answer once I find a solution (may not be ideal though considering my lack of expertise in PowerShell).Kee
Do you need to save them to a file? Will it be OK if you delete the blobs after fetching the list through get-azurestorageblob?Kee
It would be fine to delete without saving the list.Zest
Updated my answer. Please check. HTH.Kee
R
3

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

Rouen answered 10/4, 2021 at 9:53 Comment(1)
This is the simplest and clean approachUnsocial
M
1

With Azure CLI this got much easier as you can make use of az storage blob delete-batch for recursive deletion. For example for cleaning up the $web container of a static site one could use the following script:

# Define Variables
$STORAGE_ACCOUNT_NAME = 'your-storage-account-name'

# Purge Container
az storage blob delete-batch -s '$web' --account-name $STORAGE_ACCOUNT_NAME --auth-mode login

Using the pattern parameter it is possible to delete only files matching a pattern e.g. the mentioned test.zip (--pattern *test.zip).

Documentation of the Azure CLI blob delete-batch command

Marko answered 31/12, 2021 at 13:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.