Hyper-V vhdx file is much larger than the contents

Solution 1:

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!