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

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

  • December 22, 2022
  • 4 replies
  • 2948 views

Forum|alt.badge.img+1

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

This topic has been closed for comments

4 replies

Moustafa Hindawi
Forum|alt.badge.img+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

 


JeroenTielen
Forum|alt.badge.img+8
  • Vanguard
  • 1342 replies
  • December 22, 2022

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

 


JeroenTielen
Forum|alt.badge.img+8
  • Vanguard
  • 1342 replies
  • December 22, 2022

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
    }
}

 


JeroenTielen
Forum|alt.badge.img+8
  • Vanguard
  • 1342 replies
  • December 23, 2022

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