Tag : .VHD

Hyper-V: Shrinking a VHD

If you are using Hyper-V virtual machine, with thin provisioned (or dynamic) virtual disks, you may find yourself needing to compact those virtual disk files (.VHD) to reclaim free space.

Typically you would compact a virtual hard disk in situations such as the following:

  • After you install the guest operating system (which uses many temporary files)
  • After you delete large amounts of data
  • When you are preparing the virtual hard disk for archiving, for deployment to another computer or CD-ROM, or for distribution

To ensure that you get the smallest possible file size when you compact the virtual hard disk, you need to do some file system maintenance before you compact the disk. In the guest operating system of the virtual machine that is using the disk you want to compact, do the following:

  • Remove any temporary folders or unwanted folders or files.
  • Empty the Recycle Bin.
  • Defragment the disk.

Note that when running a defrag, you will actually grow the .VHD file prior to compacting, so make sure the host server has enough free space to accommodate it.

Shut down the VM, then go to the settings and go to the disk settings and click edit. This will bring up the disk edit wizard and the keep the first selected option, Compact.

Depending on the size of the .VHD file, and the available “white space” within the file, compacting may take a significant amount of time. So be sure to plan your downtime accordingly.

You may receive the error message “The system failed to compact disk.vhd. Error Code: The requested operation could not be completed due to a file system limitation” when attempting to compact the .VHD file.
First, check to see that there are no VSS backups on the VHD by running a “vssadmin list shadows” command in an elevated command prompt.

Second, delete any VSS backups that are present by running “vssadmin delete shadows /all“. If this fails to delete the VSS backups, you may need to resort to the “DiskShadow” utility in WS2K8 and WS2K8R2. (There’s some more information on DiskShadow here (free registration required) and also here.)