Skip to content
Switch branches/tags
Go to file
Cannot retrieve contributors at this time
This script will delete all blobs from the specified container over a certain age.
When given the necessary storage account information this script will find any blobs over the given age in days and
remove them. If the job fails it will send an email to the specified recipient.
This script is designed to be executed as a scheduled task.
.\Remove-OldAzureStorageBlob.ps1 -storageAccountName foo -storageAccountKey bar -containerName backups -age 30 `
-errorSMTPServer -errorSourceAddress -errorDestAddress
Author: Phil Skents (
[Parameter (Mandatory=$true,Position=1)]
[Parameter (Mandatory=$true,Position=2)]
[Parameter (Mandatory=$true,Position=3)]
[Parameter (Mandatory=$true,Position=4)]
[Parameter (Mandatory=$false,Position=5)]
[Parameter (Mandatory=$false,Position=9)]
[Parameter (Mandatory=$false,Position=5)]
$context = New-AzureStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $storageAccountKey
$date = [DateTime]::Today.AddDays(-$age)
Try {
$ErrorActionPreference = 'Stop'
Get-AzureStorageBlob -Context $context -Container $containerName | Where-Object {$_.LastModified -lt $date} | Remove-AzureStorageBlob
if (($errorSMTPServer -ne $null) -and ($errorDestAddress -ne $null) -and ($errorSourceAddress -ne $null)){
$hostname = $env:COMPUTERNAME
$sub = 'Job failed on '+$hostname
$err = $_.Exception.Message
$msg = 'Scheduled job Remove-OldAzureStorageBlob failed on '+$hostname+' with error '+$err
Send-MailMessage -From $errorSourceAddress -To $errorDestAddress -Subject $sub -Body $msg -SmtpServer $errorSMTPServer
else {
Write-Host "Error encountered" `n
Write-Host $_.Exception