A sample text widget

Etiam pulvinar consectetur dolor sed malesuada. Ut convallis euismod dolor nec pretium. Nunc ut tristique massa.

Nam sodales mi vitae dolor ullamcorper et vulputate enim accumsan. Morbi orci magna, tincidunt vitae molestie nec, molestie at mi. Nulla nulla lorem, suscipit in posuere in, interdum non magna.

VMware ESXi: howto remove the last snapshot

Sometimes we need to create a snapshot in VMware ESXi (e.g. for backup purposes) and delete it right afterwards. This kind of operations are usually performed with shell scripts. In this post, I will explain how to remove the last snapshot using the command line.

You can specify the snapshot you want to delete by using the “snapshotLevel” parameter. Snapshots are ordered from oldest to newest. The oldest snapshot has snapshotLevel=0.

First of all, you need to know your virtual machine ID. You can get the ID’s of all VM’s with the following command:

vim-cmd vmsvc/getallvms

In the next commands, I will use the variable “$VM_ID” to represent the Virtual machine ID.

To find out the number of snapshots for your VM, you can use the following commands:

vim-cmd vmsvc/snapshot.get $VM_ID | egrep -- '--\|-CHILD|^\|-ROOT' | wc -l

To remove only the last snapshot for a given VM, use the following commands:

SNAPSHOT_COUNT=`vim-cmd vmsvc/snapshot.get $VM_ID | egrep -- '--\|-CHILD|^\|-ROOT' | wc -l`
vim-cmd vmsvc/snapshot.remove $VM_ID false $(($SNAPSHOT_COUNT-1)) 0

That’s it. I suggest you to test your scripts with a dummy virtual machine. Use at your own risk! 🙂

  • Delicious
  • Facebook
  • Digg
  • Reddit
  • StumbleUpon
  • Twitter

3 comments to VMware ESXi: howto remove the last snapshot

  • What an excellent artifact. The genuine information that placed here played a very imperative role to individual specifically to those people who are met on this sort of gears. Based on my experience, the common misperception of the term “snapshot”, related to VMware, can cause huge problems. I’ve spend a lot of time the last years solving snapshot problems. For once and for all, a snapshot isn’t a static situation like a clone is. A snapshot can best be compared to a redo log, although technically it isn’t because it’s just a bitmap of disk sectors that changed. When you create a snapshot you only create a small “differences” file (*.delta.vmdk) which will contain all the differences until you delete or revert. Please remember reverting (go to) doesn’t delete the differences file! And this file can grow very fast depending on how many changes are made on the disk.When you’ve got for instance a 10 levels deep nested snapshot tree with a very large last snapshot it would almost be impossible to press delete all because it will take up a lot of disk space. It would consume a lot of time doing a “delete” for every snapshot, and still it would always take up additional diskspace. Another way to remove the snapshot is just by cloning the VM to another datastore. This way you don’t need the extra disk space on the same datastore, and it might be a good moment to consider re-loadbalancing your lun’s again. But for some weird reason this doesn’t always commit your snapshot.

  • Michiel Wortman

    Hi Marcos,

    Sounds nice but I prefer removing a snapshot with a certain name. I use these lines in a shell script where “SnapshotName” is the name given to the snapshot earlier in the script or in the VmWare Console:

    vim-cmd vmsvc/snapshot.get $VM_ID >/tmp/snapshot.txt
    vim-cmd vmsvc/snapshot.remove $VM_ID $(grep -A 1 SnapshotName /tmp/snapshot.txt | sed ‘1d’ | awk ‘{print $4}’)
    rm /tmp/snapshot.txt

    I’m sure someone with more experience will be able to put all the lines together in one command.


  • @Michiel Wortman

    Thanks for sharing! exactly what I need in my setup.


Leave a Reply




You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>