Question

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

  • 22 December 2022
  • 4 replies
  • 2103 views

Badge +1

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


This topic has been closed for comments

4 replies

Userlevel 6
Badge +8

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

Userlevel 5
Badge +6

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

 

Userlevel 6
Badge +8

Because I liked the idea I posted above :P 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
}
}

 

Userlevel 6
Badge +8

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