Share via


How to delete Azure Managed Disk snapshots older than 7 days using Powershell?

Question

Tuesday, June 27, 2017 12:11 PM

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.

All replies (5)

Monday, June 26, 2017 12:30 PM

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.


Monday, June 26, 2017 12:45 PM

See if this link helps you

https://blogs.msdn.microsoft.com/igorpag/2017/03/14/azure-managed-disks-deep-dive-lessons-learned-and-benefits/

Best Regards,Uri Dimant SQL Server MVP, http://sqlblog.com/blogs/uri_dimant/

MS SQL optimization: MS SQL Development and Optimization
MS SQL Consulting: Large scale of database and data cleansing
Remote DBA Services: Improves MS SQL Database Performance
SQL Server Integration Services: Business Intelligence


Tuesday, June 27, 2017 12:10 PM | 1 vote

Hello - It was resolved with the script below:

$rg = 'Resource Group Name'
$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(-1).tostring('yyyyMMdd'))} | remove-azurermsnapshot -force

} 

Tuesday, June 27, 2017 12:13 PM | 1 vote

Hello - managed to resolve with the code below. Thank you for the assistance


Wednesday, June 28, 2017 6:57 AM

Hi gamarcon,

 

Since this forum is discussing and asking questions about the SQL Server, and your issue is more related to Microsoft Azure, I will move this thread to the more related forum.

 

Thanks for your understanding and support.

 

Best Regards,

Teige

 

MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact [email protected].