These simple things unearth treasures like the following:
Do a full reboot after running Disk Cleanup because part of the removal happens when the system next starts up.
Zero, Defragment and Shrink
Run Sysinternals’ Sdelete tool to zero all the deleted bits on disk:
sdelete -s -z c:
While defragmenting is not strictly necessary it increases the chances that the shrinking process in the next step is successful.
Go to Disk Management and shrink your partition(s) as much as possible. This is the most important part. Without this Hyper-V unfortunately is not able to compact the VHDX significantly.
Compact VHDX from the GUI
When you go to a virtual machine’s settings and edit the hard drive, you are presented with a dialog box offering to compact the disk:
That is exactly what you want. You select it, click next and … the wizard finishes after a few (too few) seconds and the VHDX size remains unchanged.
Compact VHDX from PowerShell
I am not a big fan of dumbing down or even breaking the UI by making relevant functionality only available through PowerShell as seems to be the trend these days. This seems to be such a case. For some strange reason the Compact VHDX UI does nothing and you have to resort to PowerShell to do the job. Sad.
The relevant command is Optimize-VHD. In order for the optimization to succeed the virtual hard disk needs to be mounted first with Mount-VHD (both commands work on VHD and VHDX files).