This script deletes Azure blobs that are older than X days.

Provide the Azure Storage Account name, access key and container name as input.

A log file is created tracking the details of blobs that are deleted. Including the count of deleted blobs.

You can create an Azure Automation Runbook from this script and schedule it to run every day. So, you will not be billed for the unwanted data. This is a cost optimization strategy. 

 

 

PowerShell
Edit|Remove
## Iterate through each blob 
foreach($blob_iterator in $blob_list){ 
 
    $blob_date = [datetime]$blob_iterator.LastModified.UtcDateTime 
     
    # Check if the blob's last modified date is less than the threshold date for deletion 
    if($blob_date -le $date_before_blobs_to_be_deleted) { 
 
        Write-Output "-----------------------------------" | Out-File $local_log_file_path -Append 
        write-output "Purging blob from Storage: " $blob_iterator.name | Out-File $local_log_file_path -Append 
        write-output " " | Out-File $local_log_file_path -Append 
        write-output "Last Modified Date of the Blob: " $blob_date | Out-File $local_log_file_path -Append 
        Write-Output "-----------------------------------" | Out-File $local_log_file_path -Append 
 
        # Cmdle to delete the blob 
        #Remove-AzureStorageBlob -Container $container -Blob $blob_iterator.Name -Context $context 
 
        $blob_count_deleted += 1 
    } 
 
}