Tips and Resources

Shrink Windows Server HyperV disk VHD & VHDX size

Follow these steps to help reduce the file size of the VHD and VHDX files for your Hyper-V virtual machines

Over time, you may find that dynamically expanding VHD and VHDX files will become quite large compared to the used space inside the VM. Use this process to shrink the virtual disk size back to a size closer to the used space inside the VM.

The first step is to reduce the amount of used space on your drive

Uninstall unnecessary software

Open the Control Panel and check the Programs and Features list for applications you no longer need. Installed applications can take up to twice as much space on the disk as you anticipate as the installer files are stored in the Windows Installer Cache (typically c:\windows\Installer). Don't delete files from this folder directly, we can cleanup this folder in the next step.

Run Windows Disk Cleanup

In later versions of Windows Server, you can use Disk Cleanup to remove temp, cache and other unneeded files on your server. To do this:

  1. Open Windows explorer
  2. Right click the c:\ and choose Properties
  3. Click the Disk Cleanup button.
  4. After a few moments, a list of Files to delete will appear - review this list and tick the items you want to delete. Almost all of these are safe to remove. I'd suggest clicking on each item in the list and read the description to see if it's something that is appropriate for you to delete. Once you reviewed and selected the items you'd like to delete click OK, Click Delete Files
  5. After a moment the disk clean up window will automatically close.

Reduce the size of the WinSxS folder

Over time, the WinSxS folder will increase in size. Use this command to reset the Component Store and reduce it's size. Note this will remove your ability to rollback or remove previously installed Windows updates.

Dism.exe /online /Cleanup-Image /StartComponentCleanup /ResetBase

Determine where large files/folders reside on the disk

I recommend downloading and installing Treesize (the free version is fine) to find large folders eg log files/folders on the disk. Treesize gives you a good idea if there are other folders on the drive that are taking up a large amount of space. If you see any other large or unneeded folders such as old log folders etc, remove them and empty the recycle bin.

Once you are satisfied you've removed all you can, shutdown the server.

Defrag and Optimize the Virtual hard drive

Once the server is off, open a powershell window (on your Hyper-V server) or another PC that can access the location of the virtual machine's virtual disk and run the following command to mount the disk: 

mount-vhd <path to the virtual disk>

Make a note of the drive letter the mounted VHD is given and defrag the disk using these commands (substitute e: with the drive letter of the mounted disk): 

defrag e: /h /x /u /v /k
defrag e: /h /x /u /v
dismount-vhd <path to the virtual disk>
optimize-vhd <path to the virtual disk> -Mode Full

Virtual Hard Drive file still too big?

This process will generally yield good results, but if you find that the virtual hard drive is still quite a bit larger than the used space in the virtual machine, do the following:

  1. Boot the virtual machine again and running defrag from within the VM. Shut the VM down
  2. Open Hyper-V Manager, right click the VM and choose Settings. Click the Hard drive in the left hand pane and click Edit in the right hand pane.
  3. Click Next, Choose Compact and click Next. Once the Compact is done, reboot the VM again.

Note: This process will likely trigger a need to resynchronize your virtual machine if it's replicated to another Hyper-V sever  

References

https://docs.microsoft.com/en-us/windows-hardware/manufacture/desktop/clean-up-the-winsxs-folder

https://community.spiceworks.com/topic/2288371-hyper-v-vhdx-fragmentation

https://knowledgebase.jam-software.com/treesize_free/