Download Azure blobs to local storage and delete via Powershell

I have years and years worth of Azure blobs that are just sitting there taking up space and costing me money, and I wanted to save some money but still keep the files around just on the one-in-a-million chance that I might need one of them in the future. True, this will not save me much money, but who cares? Because software engineering…

Windows PowerShell to the rescue!

I wrote up a script to hook into my storage account and download the files to a local directory of my choosing, and then delete the file if the download is successful.

I would recommend using the latest 7.5 version of PowerShell for this process. In addition, you may need to have the Azure tools installed, which would involve running this command:

Install-Module -Name Az -AllowClobber -Scope CurrentUser

Then you need to authenticate with Azure, which would mean running this command:

Connect-AzAccount

Depending on the configuration of your Azure account, you may need to provide a tenant ID on the command line to connect up to your Azure account, it will alert you if you need to do this. This would look like this, where you would provide your tenant ID in place of the all zeros GUID below:

Connect-AzAccount -TenantId 00000000-0000-0000-0000-000000000000

Here is the script that I am using:

# Change these values for your needs
$resourceGroupName = "resourcegroup"  # change
$storageAccountName = "accountname"  # change
$containerName = "containername"  # change
$localDownloadPath = "C:\Downloads"  # change
$startDate = [datetime]"2019-01-01T00:00:00Z"  # change
$endDate = [datetime]"2019-12-31T23:59:59Z"  # change
 
$storageAccountKey = (Get-AzStorageAccountKey -ResourceGroupName $resourceGroupName -Name $storageAccountName)[0].Value
$ctx = New-AzStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $storageAccountKey
$blobs = Get-AzStorageBlob -Container $containerName -Context $ctx
$filteredBlobs = $blobs | Where-Object { $_.LastModified -ge $startDate -and $_.LastModified -le $endDate } | Select-Object -First 500  # change
foreach ($blob in $filteredBlobs) {
    $blobName = $blob.Name
    $localFilePath = Join-Path -Path $localDownloadPath -ChildPath $blobName
    Write-Host "Downloading $blobName (Last Modified: $($blob.LastModified))..."
    Get-AzStorageBlobContent -Container $containerName -Blob $blobName -Destination $localFilePath -Context $ctx -Force
    if (Test-Path $localFilePath) {
        Write-Host "Deleting $blobName from Azure Storage..."
        Remove-AzStorageBlob -Container $containerName -Blob $blobName -Context $ctx
    } else {
        Write-Host "Error: File $blobName was not downloaded successfully. Skipping deletion."
    }
}
 
Write-Host "Process completed."

You will need to change the variables marked with # change in the first section of this form, and then also modify the limit of 500 in the $filteredBlobs line if you want to do more or less files at one time.

BTW, Happy Independence Day to all my readers in Saint Lucia.

Leave a Reply