Dynamically Expanding VHD not Compacting in Hyper-V


OK, I feel duty bound to report this as I only accidentally found an answer, so hopefully this’ll help someone else.

Long story short – Hyper-V compact doesn’t work, but “Diskpart Compact vDisk” command does!

So, you have a massive Hyper-V VHD or VHDX file and you need to shrink it down. So you open the Hyper-V toold, go to “Edit Disk” compact, wait the 4 billion years or so that it takes to finish and look at your VHD size aaaannddd… No change! Hyper-V is not compacting your VHD file at all, it’s still the same size, or in my case, slightly larger!

In trying to find a solution a lot of pages at this point suggest setting up a new VHD and copying the contents across before deleting the old one – in my case I couldn’t do this because the VHD was well over half of my available disk space, and it was big – over 1TB so it’s not trivial to be able to chuck a bit more disk space in to accomodate it.

So, anyway turns out that you can compact a VHD or VHDX using the Diskpart command. Now, I’m not much of a command line guy but I have to say I’ve grown quite fond of diskpart, it’s a really well written and versatile tool. So it’ll crunch your VHD’s where Hyper-V fails, and the kicker is that it took about 1/8th the time the proper Hyper-V tool took. :-/

The procedure is like this:

Run an administrative command prompt
Type “Diskpart” (No Quotes)
Type the following commands:

select vdisk File=”I:\path\to\your.vhd”
attach vdisk readonly
compact vdisk
detach vdisk

That’s it. My particular VHD had been set to be a “dynamic volume” in it’s Virtual Machine’s disk management (It was a dynamically expanding VHD, but also, in disk management of its VM it was further configured as a “Dynamic Volume”) Perhaps that had something to do with it, but in all honesty I don’t know why this works and Hyper-v fails to compact, and I don’t know if it’ll work in your case, but it got me out of a hole.

Good Luck!

12 Comments

  1. Mike says:

    Been trying to figure this out forever. Thank you so much!!

  2. BEST HR says:

    on minor change in syntax for a hyper-v 2012 based machine:
    select vdisk file=“I:\path\to\your.vhd”

  3. Sandra Madera says:

    Great help, thank you. But also, please note @BEST HR’s comment above, it is necessary to change the syntax to include FILE=

  4. Robert says:

    Why the “readonly” option?

    1. fiddley says:

      It’s because you don’t want the host system where you attach the disk to modify any files. It’s just a consolidate and shrink without touching the files on the disk.

  5. Tamer Kamal says:

    Thank you very much , awesome!

  6. Phillip Evans says:

    Thanks! This ran much faster than Hyper-V GUI compaction, and it actually worked, where Hyper-V disk compaction did nothing to reduce the VHD file size.

  7. John says:

    Will this work in Server 2012 R2? Compacting fails when I use the GUI

    1. fiddley says:

      It has worked for me more often than not. It does fail still sometimes, notably when I’ve enabled Deduplication as for some reason it stores the dedupe data mostly at the end of the drive.

  8. Fantastic – i just shrink file from 1 TB to 240 GB. Thank you man!

  9. rwt says:

    This seems like the best option for Windows 10 Home as hyperv is in there or installable.

Leave a Comment