NetApp Community Update
This site will enter Read Only mode on July 23 as we prepare to move to a new platform. You will still be able to view content, but posting and replying will be temporarily disabled.
We're excited to launch our new Community experience on July 30 and more information will follow soon.
Stay connected during the transition - Join our Discord community today.

Microsoft Virtualization Discussions

Script to delete all but a specified number of snapshots on all vols/aggrs

itayemi
6,769 Views

Hi All,

I tried to write a PowerShell script to connect to a filer, and delete all (from the oldest first) but a configurable number of snapshots on all the volumes and aggregates on the filer. I think it was probably too much of a task for my very first powershell scripting attempt. I tried debugging the script using powershell_ise but I am definitely missing a lot on how to use the DataOntap module/calls. Can anyone help?

- keepsnaps=5

- connect to filer

- retrieve all vols/aggr

- for every vol/aggr, delete all snapshots from the oldest leaving the most recent $keepsnaps number of snapshots from all vols and all aggregates

Thanks.

3 REPLIES 3

itayemi
6,770 Views

Hi All,

In the meantime, I have implemented the script in bash.

I am uploading it (delete_all_but_n_snapshots.sh) in case anyone is interested.

I am still interested in a powershell script to do the same.

Thanks.

glenn_sizemore
6,770 Views

Something to get you started.

$max = 5

foreach ($vol in Get-navol)
{
    $Snapshots = Get-NaSnapshot -Name $vol.name
    $remove = $Snapshots.Count - $Max
    if ($remove -ge 1)
    {
        $Snapshots |
            Sort-Object AccessTime |
            Select-Object -First $remove |
            Remove-NaSnapshot
    }
}

~Glenn

dcsanadmin
6,770 Views

I need a script to delete snapshots that are more than 30 days old. Our backup software is supposed to do it but it fails every now and then leaving old snapshots on our  filers. As I know absolutely nothing about scripting I would really appreciate your help in coming up with a script to meet our needs.

Thanks in advance for your help.

Karon

Public