6

I have a virtual machine, that has dynamically expanding vhdx as a C drive. Or 'thin provisioned' to speak in terms of other hyper-visors.

I'm running Windows server 2016 on it, as a DC to be exact. Over the time of two years the vhdx file grew; the size of the disk within the virtual machine lists as 18gb, while the vhdx file is around 128gb. The vhdx is to big.

What caused this?, and how can I shrink the size of the vhdx file.

Dennis
  • 756

2 Answers2

7

After a few days of trying several things, a combination of the following helped to shrink the vhdx file;

  1. In the virtual machine; properties of the drive, do a disk clean up, reboot
  2. Open an powershell as admin and do a: Optimize-Volume -DriveLetter C -Defrag -Verbose
  3. In the same powershell do a; Optimize-Volume -DriveLetter C -ReTrim -Verbose for some wierd reason, I had to repeat these steps a few times. Once it even crashed.
  4. Shut down the virtual machine, and in the hyper-v manager; go to the virtual machine, go to the virtual disk. Edit, choose action; compact.

Finally, the vhdx file was around the size of the actual data on it. I hope this helps others!

Dennis
  • 756
0

I personally don't have any luck with the Hyper V disk compact option or the PowerShell Optimize-VHD cmdlet. But what worked was to use a disk space analyzer in the VM such as gdu in a Linux VM and find out the total space being consumed. Then power off the VM and use Hyper V disk editor to shrink the capacity of the vhdx to one or two GBs more than the total disk space consumed. It will now resize the disk file to that size. Then just extend the capacity again to any large value / previous value.