How to delete Azure Managed Disk snapshots older than 7 days using Powershell?
We take daily automated snapshots of the Managed Disks. The snapshots are named as: ['AppDisk_snapshot_AM' + "_" + (Get-Date -Format "yyyy-MM-dd")]
Snaphoshots are stored on "/subscriptions/[subscription ID]/resourceGroups/[Resource Group Name]/providers/Microsoft.Compute/snapshots"
I would like to know how can I delete these snapshots that are older than 7 days and keep the newest. Thank you.
According to your description, we can use this script to do this:
$rg = 'vm'
$snapshotnames = (Get-AzureRmSnapshot -ResourceGroupName $rg).name
foreach($snapname in $snapshotnames)
{
Get-AzureRmSnapshot -ResourceGroupName $rg -SnapshotName $snapname |?{$_.id -like '*AppDisk*'} | ?{($_.TimeCreated).ToString('yyyyMMdd') -lt ([datetime]::Today.AddDays(-7).tostring('yyyyMMdd'))} | remove-azurermsnapshot -force
}