Compact a Dynamic VHDX with DiskPart

VHDX files can take up a lot of unnecessary disk-space, especially if the disk is used for a lot of writes. A dynamic vdisk expands, but does not automatically compacts again.

Sometimes it is necessary to first defragment the disk. I use PerfectDisk V14 for this, but there are a lot of defragmentation tools around that work just as well. After the VHDX is defragmented and optimized for shrinking, open a command prompt with administrator rights and start DiskPart:

Type the following commands:

  • select vdisk file=”<path to the virtual disk>”  (quotes are mandatory if there are spaces in the path)
  • attach vdisk readonly
  • compact vdisk
  • detach vdisk
  • exit

The disk will now have shrunk to the size that the data on the disk is using (if defragemented properly).

Leave a Reply

Your email address will not be published.