SQL SERVER – Previewing Old Azure Backup Blobs Before Removal with PowerShell

Backup Files From Azure Storage need a retention review before removal. I list the candidates before approving any cleanup.

Intact archive cases are set aside for inspection while the retained collection remains secure.

A reader shared this alternative to my earlier Azure backup-cleanup script. It selected .bak blobs at least ten days old. The age calculation is useful, but the restore chain must determine what can be removed.

The original Azure cmdlets have been replaced by the Az module equivalents. This updated Windows PowerShell example lists candidate .bak blobs using UTC timestamps and an existing approved account context. It contains no storage-account secret.

# Article example only. Requires an existing approved Az sign-in and RBAC access.
$storageAccount = 'YourStorageAccount'
$containerName = 'YourBackupContainer'
$retentionDays = 10
$cutoffUtc = [DateTimeOffset]::UtcNow.AddDays(-$retentionDays)
$storageContext = New-AzStorageContext -StorageAccountName $storageAccount -UseConnectedAccount
$candidates = @(Get-AzStorageBlob -Container $containerName -Blob '*.bak' -Context $storageContext |
    Where-Object { $null -ne $_.LastModified -and $_.LastModified -lt $cutoffUtc })
$candidates | Select-Object Name, LastModified, Length
# Review the full/differential/log restore chain and retention policy first.
# This remains a preview, even if uncommented:
# $candidates | ForEach-Object {
#     Remove-AzStorageBlob -Container $containerName -Blob $_.Name -Context $storageContext -WhatIf
# }

Age and the .bak extension don’t establish that a backup is disposable. An older full backup can support retained differentials or logs. I check restore tests and the required chain. Storage immutability, snapshots and versioning need separate review.

The commented removal block uses -WhatIf and remains a preview. Actual cleanup needs a separately approved list and an intentional removal step. Azure lifecycle rules can automate age-based removal, but those rules also need the same backup-retention design.

The script can run from an authorized Windows management machine. It doesn’t have to run on the database server. Use an approved Azure account context for the listing.

Reference: Remove-AzStorageBlob preview and deletion behavior.

Related reading

A file age is not a restore-retention decision, it is one attribute used to inspect a cleanup candidate.

Published by Pinal Dave on SQLAuthority. More of my work at pinaldave.com.


Discover more from SQL Authority with Pinal Dave

Subscribe to get the latest posts sent to your email.

PowerShell, SQL Azure, SQL Data Storage, SQL Scripts, SQL Server
Previous Post
Reverting a Failed Deployment With a Database Snapshot
Next Post
SQL SERVER – Upgrade Failure – The Cluster Resource is Not Online. Bring the Analysis Services Server Online Before Starting the Upgrade Process

Related Posts

2 Comments. Leave new

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.