How to Compact a Dynamic VHD with Diskpart

How to Compact a Dynamic VHD with Diskpart




Compacting a expandable/dynamic VHD was a mystery for me until I found those easy steps. You don't need any Hyper-V roles to make those steps. Some other products use the VHD standard, so that only show me how good is implanted the VHD standard when built-in tool easilly manage the format. (need-work tag added, if someone got some en-US screenshot to change mine)



What to do

 

Open a command prompt

diskpart

 


select vdisk file="d:\store\vdisk.vhd"

 

 

 

attach vdisk readonly
 
compact vdisk
 
detach vdisk


exit



Result


Before: (8.2GIG)



After: (7.3GIG)




Optional step


Run sdelete tool from Microsoft in the VM before compacting the VHD. (sdelete -c)

Leave a Comment
  • Please add 5 and 3 and type the answer here:
  • Post
Wiki - Revision Comment List(Revision Comment)
Sort by: Published Date | Most Recent | Most Useful
Comments
  • Ed Price - MSFT edited Revision 2. Comment: Title casing and tag. Great article!

Page 1 of 1 (1 items)
Wikis - Comment List
Sort by: Published Date | Most Recent | Most Useful
Posting comments is temporarily disabled until 10:00am PST on Saturday, December 14th. Thank you for your patience.
Comments
  • Ed Price - MSFT edited Revision 2. Comment: Title casing and tag. Great article!

  • Thanks for this concise article, and the tip on using sdelete to clean free space in the VHD.

    If you look at the sdelete syntax technet.microsoft.com/.../bb897443 (under "Using SDelete"), you'll notice that the -V switch is specific for cleaning VHD files.z

    So you would run 'sdelete.exe -z V:' [where V: is the drive letter of the mounted VHD file] and then repeat the steps above to compact the VHD.

  • Cool !!!

  • So using sdelete made my .VHD file grow from 85GB to 120GB.  Diskpart compact completed successfully but did not result in a smaller file.  Following these steps expanded my VHD and made my problem worse.  I now have a completely full host drive...

  • Shan, sorry to hear that. Actually when I did try the sdelete pass, the file grow at first, because it fill empty space with 0 (thus the file grow to it maximum's possible's size), but another compact did the job on my side to simply shrink it.

  • Shan, sorry to hear that. Actually when I did try the sdelete pass, the file grow at first, because it fill empty space with 0 (thus the file grow to it maximum's possible's size), but another compact did the job on my side to simply shrink it.

Page 1 of 1 (6 items)