info@techdevops.com | 437-991-3573 | Data Engineering Services
TechDevOps.com
Resources Tools
Experts in Microsoft SQL Server on Windows, Linux, Containers | Clusters, Always On, FCI | Migrations, Cloud, Performance



Delete Microsoft Azure Storage Blob(s) based on specified retention period
by BF (Principal Consultant; Architecture; Engineering)
2016-06-15








Solution:


This Powershell code will authenticate to Azure, connect to the proper Subscription and delete Blog file(s) based on
the extension names .bak and .log. It will delete based on a user specified retention period.

In the code below, $RetentionPeriodInHours will hold the value set for number of hours. ex. "168" means delete all
greater than 7 days old.

You will need your Microsoft Azure Subscription Name, Storage Account Name, Storage Account Access Key & Blog Container Name.


Powershell Code:


Login-AzureRmAccount

Set-AzureRmContext -SubscriptionName "<InsertAzureSubscriptionNameHere>"

$StorageAccount = "<InsertAzureStorageAccountNameHere>"
$StorageAccountKey = "<InsertAzureStorageAccountAccessKeyAccessHere>"
$StorageAccountContainer = "<InsertAzureStorageAccountBlogContainerNameHere>"

$Context = New-AzureStorageContext -StorageAccountName $StorageAccount -StorageAccountKey $StorageAccountKey

$RetentionPeriodInHours = [DateTime]::UtcNow.AddHours(-168)
Get-AzureStorageBlob -Container $StorageAccountContainer -Context $Context | Where-Object
{$_.LastModified.UtcDateTime -lt $RetentionPeriodInHours -and $_.BlobType -eq "PageBlob"
-and $_.Name -like "*.bak"} | Remove-AzureStorageBlob

$RetentionPeriodInHours = [DateTime]::UtcNow.AddHours(-168)
Get-AzureStorageBlob -Container $StorageAccountContainer -Context $Context | Where-Object
{$_.LastModified.UtcDateTime -lt $RetentionPeriodInHours -and $_.BlobType -eq "PageBlob"
-and $_.Name -like "*.log"} | Remove-AzureStorageBlob



If you have issues running the above Powershell code, you will likely need to download Azure Powershell Cmdlets. A quick method for this is to download Microsoft Web Platform Installer and then install Azure Powershell from it.

We placed the above Powershell Code inside a SQL Server Job and used that to purge SQL Backups > specified days.


Notes:

If you have any Blobs that have an active lease you will receive the below error in Powershell ISE:




If you go to Azure Portal and try to delete that specific blob, it will show the error there also:




To fix this issue, you can download CloudXplorer. Once installed, add your Azure Storage Account, browse to the Blob Container & locate
the Blob files that have the active lease and on each file select break lease.



Once the lease is broken, Powershell ISE will then be able to delete those blob files also.


That's it!



Resources:

Microsoft Azure

Get started with Azure PowerShell cmdlets

CloudXplorer