Execute this script maybe meme

How to selectively remove items from an Azure blob storage container?

3 min read.

In this article, I’ll share a simple script for a simple use case: removing blobs from a virtual directory structure in a container in an Azure blob storage account.

This should be useful for a case like ours; removing certain directories and subdirectories but not the whole container.

But let’s not get ahead of ourselves! What was the problem we had?

Background

So a few days ago, the resident sysadmin (pictured below) came to me with a problem: He had a storage account in Azure, that had been used for storing backups, but apparently also for some virtual machine images and likely also as a mounted file share – so it had a lot of random stuff. Some was trash, some used to be helpful but wasn’t anymore, but some was content worth sharing.

He had already catalogued it and figured what was valuable and what wasn’t, and he was just left with the task of removing a few folders with thousands and thousands of small files from a container that also had stuff that needed to be conserved.

The easy thing was to figure out which folders needed to be saved and which didn’t. But the difficulties started when we were supposed to start removing the folders.

Get this: you cannot remove a folder in blob storage.

… and on second thought, of course you can’t. Blob storage doesn’t have structure – and it doesn’t have folders or directories, either. That’s just UI sugar added on top of a naming convention.

See, if you name your blob test/blob, it’ll show up in a virtual folder called “test”. And if you then click into the folder, you’ll see its contents – your blob.

And when you remove the blob? The folder disappears. Because any directory structure is a lie. It’s all virtual and only shown if the contents exist.

… and the folder disappears altogether.

Ok! Well this looks like a scriptable thing, right?

Solution

Azure PowerShell to the rescue! There’s always a commandlet, and now we just need to find the right commandlet to find blobs belonging to the right virtual directories – which is just based on naming.

And this brings us to our solution. In blob storage, you can’t remove folders, because they don’t exist, but if you remove all blobs within, the folders disappear too.

And how do you recognize the blobs residing within certain virtual directories? By using the -Prefix parameter for Get-AzStorageContainer!

-Prefix
Specifies a prefix for the blob names that you want to get. This parameter does not support using regular expressions or wildcard characters to search. This means that if the container has only blobs named “My”, “MyBlob1”, and “MyBlob2” and you specify “-Prefix My*”, the cmdlet returns no blobs. However, if you specify “-Prefix My”, the cmdlet returns “My”, “MyBlob1”, and “MyBlob2”.

Source:
https://learn.microsoft.com/en-us/powershell/module/az.storage/get-azstorageblob?view=azps-10.4.1

The script below will remove all blobs from a specified container in an Azure storage account

How to remove all blobs from a virtual directory in an azure storage account

Update-AzConfig -DisplayBreakingChangeWarning $false
$ctx = New-AzStorageContext -StorageAccountName 'contosovmstorage' -StorageAccountKey 'yourKeyIsActuallyLongerThanThis=='

# Configure your blob listing's filtering (which container and folder to use)
$containerName = "vhds"
$prefix = "test/"

$deleted = 0

$container = Get-AzStorageContainer -Name $containerName -Context $ctx 

Write-Host "Processing" $container.Name -ForegroundColor Green

$blobs = Get-AzStorageBlob -Container $container.Name -Context $ctx -Prefix $prefix
foreach ($blob in $blobs) {
    Write-Host "  - Processing blob " $blob.Name "..." -ForegroundColor Yellow
    Remove-AzStorageBlob -Blob $blob.Name -Container $container.Name -Context $ctx -Confirm:$false
    $deleted++;
    Write-Host "  - Done" -ForegroundColor DarkYellow
}

Write-Host "All Done! Deleted "$deleted" blobs. Now get some coffee, you've earned it."

That’s all for today :)

References

mm
0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments