What is the Best way to Delete Old Snapshots of VMs autmatically? | Nutanix Community
Skip to main content

What is the Best way to Delete Old Snapshots of VMs autmatically?

Hello @Kuldeep@ 

Create snapshot via Protection Domain as below from PE > Data Protection , then input how many snapshot do you need to keep, it will keep the newest and delete the old

 


If the snapshots are already created you can get a list with the powershell script in this blogpost: https://next.nutanix.com/how-it-works-22/list-current-snapshots-via-a-script-32693

With that script you have some pointers to cleanup the snapshots with the cmdlet remove-ntnxsnapshot

 


Because I liked the idea I posted above 😛 Hereby and updated script to automatic delete snapshots older then the given days. 

Change pe-ip, username and password. And set the $DeleteAfterDays to something you like. 

 

Connect-NutanixCluster -Server <PE-IP> -UserName admin -Password nutanix/4u -AcceptInvalidSSLCerts -ForcedConnection

$DeleteAfterDays = 30
$NutSnaps=Get-NTNXSnapshot
$AllNutVMs=Get-NTNXVM | Select UUID,VMName

ForEach ($Snap in $NutSnaps) {
$VMName= ($AllNutVMs | Where {$_.UUID -eq $Snap.VMUUID}).VMName
$SnapTime=$Snap.createdtime/1000
$SnapDt=(Get-Date '1/1/1970').AddMilliSeconds($SnapTime)
$ActualSnapDt=$SnapDt.ToLocalTime()
$Limit = (Get-Date).AddDays(-$DeleteAfterDays)
If ($Limit -lt $ActualSnapDt) {
Write-Host "Snapshot named " -NoNewline
Write-Host $Snap.SnapShotName -ForegroundColor Yellow -NoNewline
Write-Host " on VM " -NoNewline
Write-Host $VMName -ForegroundColor Yellow -NoNewline
Write-Host " created on " -NoNewline
Write-Host $ActualSnapDt -ForegroundColor Yellow -NoNewline
Write-Host " is within the limit and will not be deleted."
} Else {
Write-Host "Snapshot named " -NoNewline
Write-Host $Snap.SnapShotName -ForegroundColor Red -NoNewline
Write-Host " on VM " -NoNewline
Write-Host $VMName -ForegroundColor Red -NoNewline
Write-Host " created on " -NoNewline
Write-Host $ActualSnapDt -ForegroundColor Red -NoNewline
Write-Host " is older then the limit and will be deleted."
$Remove = Remove-NTNXSnapshot -UUID $Snap.UUID
}
}

 


Update: Made a better version and created a blog post for it: https://www.jeroentielen.nl/cleanup-nutanix-snapshots-after-amount-of-days/