azure Azure-Automation Delete Blobs in Blob storage older than a number of days


Here is an example of an Azure Powershell automation runbook that deletes any blobs in an Azure storage container that are older than a number of days.

This may be useful for removing old SQL backups to save cost and space.

It takes a number of parameters which are self explanatory.

Note: I have left some commented out code to help with debugging.

It uses a service principal that Azure can set up for you automatically when you create your automation account. You need to have Azure Active Directory access. See pic:

    Removes all blobs older than a number of days back using the Run As Account (Service Principal)

    AUTHOR: Russ
    LASTEDIT: Oct 03, 2016   #>



    # StorageAccount name for content deletion.
    [Parameter(Mandatory = $true)] 

    # StorageContainer name for content deletion.
    [Parameter(Mandatory = $true)] 

    [Parameter(Mandatory = $true)]

$VerbosePreference = "Continue";
# Get the connection "AzureRunAsConnection "
$servicePrincipalConnection=Get-AutomationConnection -Name $connectionName         

"Logging in to Azure..."
Add-AzureRmAccount `
    -ServicePrincipal `
    -TenantId $servicePrincipalConnection.TenantId `
    -ApplicationId $servicePrincipalConnection.ApplicationId `
    -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint 
catch {
if (!$servicePrincipalConnection)
    $ErrorMessage = "Connection $connectionName not found."
    throw $ErrorMessage
} else{
    Write-Error -Message $_.Exception
    throw $_.Exception
$keys = Get-AzureRMStorageAccountKey -ResourceGroupName $resourceGroupName -AccountName $StorageAccountName
# get the storage account key
Write-Host "The storage key is: "$StorageAccountKey;
# get the context
$StorageAccountContext = New-AzureStorageContext -storageAccountName $StorageAccountName -StorageAccountKey $keys.Key1 #.Value;
$existingContainer = Get-AzureStorageContainer -Context $StorageAccountContext -Name $ContainerName;
if (!$existingContainer)
 "Could not find storage container";
$containerName = $existingContainer.Name;
Write-Verbose ("Found {0} storage container" -f $containerName);
$blobs = Get-AzureStorageBlob -Container $containerName -Context $StorageAccountContext;
$blobsremoved = 0;

if ($blobs -ne $null)
    foreach ($blob in $blobs)
        $lastModified = $blob.LastModified
        if ($lastModified -ne $null)
            #Write-Verbose ("Now is: {0} and LastModified is:{1}" –f [DateTime]::Now, [DateTime]$lastModified);
            #Write-Verbose ("lastModified: {0}" –f $lastModified);
            #Write-Verbose ("Now: {0}" –f [DateTime]::Now);
            $blobDays = ([DateTime]::Now - $lastModified.DateTime)  #[DateTime]

            Write-Verbose ("Blob {0} has been in storage for {1} days" –f $blob.Name, $blobDays);

            Write-Verbose ("blobDays.Days: {0}" –f $blobDays.Hours);
            Write-Verbose ("DaysOld: {0}" –f $DaysOld);

            if ($blobDays.Days -ge $DaysOld)
                Write-Verbose ("Removing Blob: {0}" –f $blob.Name);

                Remove-AzureStorageBlob -Blob $blob.Name -Container $containerName -Context $StorageAccountContext;
                $blobsremoved += 1;
            else {
                Write-Verbose ("Not removing blob as it is not old enough.");

Write-Verbose ("{0} blobs removed from container {1}." –f $blobsremoved, $containerName);

It you use the test pane you can enter the required parameters and run it.

enter image description here

As you can see, when I ran it, it didn't find any blobs that were old enough to delete.