Microsoft Virtualization Discussions

remove snapshots by count

JSHACHER11
4,037 Views

the following will remove snapshots older than 5 days - how do I do the same with number of snapshots? (keep only 5 snapshots)

Get-NaVol vol2 | Get-NaSnapshot | where-object {$_.Created -lt (Get-Date).AddDays(-5)} | remove-nasnapshot -Confirm:$false

thanks

7 REPLIES 7

cscott
4,037 Views

Joel,

     Here is a quick and dirty way to create an array of snapshots.  They are sorted by date, oldest to newest, then you could use the array to delete the snaps you require -

Connect-NaController $myNetAppNode -Credential $myNetAppCred -HTTP

$NetAppSnapArray = @(Get-NaSnapshot -TargetName $myNetAppVolume |sort -Property Created | % {Write-Output $_.Name})

to see the snaps add - write-output $NetAppSnapArray

You can then use this array to further manipulate the snaps.  In this example we use it to list out all the snaps(in a text menu) on the volume so we can figure out the snap delta to see how much data a given snapvault or snapmirror has to transfer.  I have attached the entire script if you want to see what I am talking about.

-Scott

JSHACHER11
4,037 Views

thanks Scott

how would that give me the number of snapshots in a volume though?

JSHACHER11
4,037 Views

it would have to be done with 'measure' somehow

I get the count with the following:

Get-NaVol vol1 | Get-NaSnapshot | measure

that gives me a 'count' (number of snapshots)

count: 5

with this in mind, I need to figure out how to move forward..

JSHACHER11
4,037 Views

maybe something like:

{$_.count -gt "3"} | Remove-NaSnapshot

cscott
4,037 Views

Hi Joel,

     With an array, you have the ability to see how "long" it is.  $SnapNameArray.length will tell you how many snapshots are in the array.  Now if you want to keep five, you would need to build a loop to remove the unwanted snapshots.  Now the issue you would have with what I put up is that the oldest snapshosts are on top, and it is easiest to manipulate the array from the bottom. You can use this -  [array]::Reverse($NetAppSnapArray)

Now, all you have to do is remove the last item in the array using the loop of your choice to call the last snapshot from the array and delete it until the length of the array = 5.

I don't have the exact code, I can try to put something together, but I at least wanted to give you a path to start looking into.

-Scott

JGPSHNTAP
4,037 Views

I'm just curious, why are you wanting to remove snapshots by count or greater than..

cscott
4,037 Views

Joel,

     This sure isn't pretty, but it works.  This will get the snaps on a given volume in order of created date.  It will then reverse the array order to ensure we get oldest first.  From there, by setting "$i =5"  We say that anything above item 4 in the array(arrays start at 0 so position 4 is the fifth item), remove that.  I recommend testing this somewhere safe just to be sure it works as you expect.

-Scott

Connect-NaController $myNetAppNode -Credential $myNetAppCred -HTTP

$NetAppSnapArray = @(Get-NaSnapshot -TargetName $myNetAppVolume |sort -Property Created | % {Write-Output $_.Name})

[array]::Reverse($NetAppSnapArray)

$x = $NetAppSnapArray.length

for ($i = 5; $i -lt $x; $i++) {

Remove-NaSnapshot -TargetName $myNetAppVolume -SnapName $NetAppSnapArray[$i]

}

Public