New
#1
I have discovered an easy way to compacting VHDs
As we all know, vhds grow in physical storage size even if the vhds are much smaller in content inside the vhd.
For example, I had a PRO 10 1709 VHD of approx. 18GB and upgraded it to 1803. After I did disk cleanup, to remove windows.old etc, the contents of the vhd are back to 18GB but physically the vhd on hard drive was 38GB.
I ran compact from diskpart (and from Hyper-V on a copy) and it had little effect, shrinking to around 36GB.
I googled it and found a utility called SDELETE that zeros unused space (compact not work on unzeroed space apparently). I ran this on a 38GB copy, and it had some effect reducing to 27GB.
However, I know with a bit of effort, shrinking C drive using minitool partition wizard, cloning vhd to another vhd, and re-expanding, the new vhd only occupies 18GB. I have done this many times.
Then I had a light bulb moment - I thought why does the cloning method work, and I realised the key step is the shrinking step as that is essentially doing a defrag.
So I thought "what if I defrag the 38GB vhd, and then compact?"
I did this and, flabber my gast, it worked first time with the existing vhd being reduced to 18GB!
The defrag of 38GB in a 50GB volume only took few minutes as the VHD was on an SSD (I am not defragging the SSD, only the virtual drive).
So here is how to do it
1) Mount vhd as a drive and note letter of OS drive e.g. R:
2) Open admin command prompt and type
Code:defrag R: /u
3) Eject mounted vhd from disk management
4) Compact VHD (substitute correct drive letter, name and path as appropriate)
Compact from Hyper-V does not work after a defrag - it thinks file is compact.Code:diskpart select vdisk file="D:\my path\my vhd.vhdx" compact vdisk exit
Last edited by cereberus; 28 Aug 2018 at 12:39.