Why does it take so long to delete a virtual machine with snapshots? [Hyper-V]

You may have noticed that if you try to delete a virtual machine with snapshots associated with it – it can take quite a long time for the virtual machine to complete deleting.  The reason for this is that we are “cleaning up” the snapshot files – which may involve merging AVHD files.

Let’s imagine that you had performed to following steps:

  1. Created a blank virtual machine
  2. Installed Windows Vista
  3. Taken snapshot “Windows Installed”
  4. Installed Microsoft Office
  5. Taken snapshot “Office Installed”
  6. Written your life's memoirs
  7. Taken snapshot “Finally done”
  8. Installed Windows Live
  9. Taken snapshot “Live installed”
  10. Applied snapshot “Finally done”

It is our belief that if you were to delete the virtual machine at this point in time – you would expect the virtual hard disk that is left over to contain Windows, Office and your memoirs – but not Windows Live.  To do this we need to merge in all of the changes from the first three snapshots, which can be quite a time consuming operation.  Essentially what we are doing is deleting each of the snapshots first (which can result in merging of AVHDs) and then deleting the virtual machine when that operation is finished.

The alternate options would be:

  1. Discard all the AVHD files.  Which in this case would leave you with a VHD with just Windows installed on it.
  2. Leave the AVHD files where they are.  Unfortunately – you may not always know where these files are, which could be quite problematic for cleaning up later.

If you have a virtual machine with snapshots that you want to delete quickly – and you do not care about the data that is stored in the virtual machine snapshots – you can do so by simply applying the first snapshot (in the above case “Windows Installed”) before deleting the virtual machine.

Cheers,
Ben