Delete VM Snapshots in PowerCLI
Delete VM Snapshots in PowerCLI
To view the snapshots for a Virtual Machine before you remove them, you can type the following command replacing VMname with the name of your Virtual Machine:
Get-Snapshot VMname | select name,id
In the command above, I select to see the name and id which will give you a list of the servers snapshot names and snapshot IDs. The ID will be important to note when being asked to confirm the removal of the snapshot when running the “Remove-Snapshot” cmdlet.
To remove the snapshots found in the command above, we can use the “Get-Snapshot” cmdlet and pipe it to the “Remove-Snapshot” cmdlet as shown below, replacing VMname with the name of your Virtual Machine (This will remove all snapshots for the specified Virtual Machine):
Get-Snapshot VMname| Remove-Snapshot
You will then be asked to confirm the deletion of the snapshot (you will see that the confirmation uses the ID that we selected to view earlier). Type “Y” and press enter to confirm the removal. If you did not want to be asked to confirm the removal you could use the “confirm” parameter as shown on the command below:
Get-Snapshot VMname| Remove-Snapshot -confirm:$false
If you wanted to see what happens by typing the command above without having any snapshots removed, the “Remove-Snapshot” cmdlet supports the use of the “whatif” parameter. You could type the following to see what will happen without actually removing anything:
Get-Snapshot VMname| Remove-Snapshot –whatif
If you wanted to remove snapshots for multiple VMs and you had your Virtual Machines organized in folders, you could type the following command to remove all snapshots for all Virtual Machines located in the “My Lab” folder:
Get-VM -location “My Lab” | Get-Snapshot | Remove-Snapshot -confirm:$false
In the above command, we use the “Get-VM” cmdlet to get all of the Virtual machines in the “My Lab” folder. We then pipe it to the “Get-Snapshot” cmdlet to get the snapshots for the Virtual machines in the “My Lab” folder. Next we pipe this to the “Remove-Snapshot” cmdlet to remove all snapshots found on Virtual Machines in the “My Lab” folder.
If you wanted to remove all VM snapshots regardless of folder location, you could remove the -location parameter from the command in the last example. This will remove all snapshots for VMs that have them. The snapshots will be removed from each VM one by one. The command is as follows:
Get-VM | Get-Snapshot | Remove-Snapshot -confirm:$false