Stuff I found out today…

Dynamically Expanding VHD not Compacting in Hyper-V

Posted on: January 27, 2014

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!


9 Responses to "Dynamically Expanding VHD not Compacting in Hyper-V"

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

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

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

Why the “readonly” option?

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.

Thank you very much , awesome!

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.

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

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.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Bung in your email addy and get yet more spam in your inbox!

Join 2 other followers

%d bloggers like this: