Main Content

Windows: How To Compact A Dynamic VHD

Making use of dynaminc VHDs or Virtual Hard Disks has the benefit of growing along with the users need. The downside is that even after files are deleted from a dynamic VHD it will remain the same size as if the files where never deleted.

This becomes a problem if you make use of multiple VHDs because you are essentially wasting space on files that no longer exist. The solution is to Compact the VHD using Diskpart a tool provided with Windows.

Start Compacting

Open a command line window and type:


Select the disk you wish to compact by specifying the drive where the file is located and the path:

select vdisk file="F:DAT100GB.vhd"

Attach the disk as Read-only:

attach vdisk readonly

And issue the command to compact:

compact vdisk

Depending on the size of the drive the operation may take some.

Detach the VHD:

detach vdisk

In my case after compacting a 100GB VHD with 20GB of deleted data the size decreased to 82.2MB, Dynamic VHDs are useful but can also become the reason why disk space is low. If you have any questions leave a comment below.


  1. Commented on
    Comment by Keith


    I have a SQL server running in HyperV. With the backups it was running, it grew to about 500gb. Ive since moved or deleted all those backup files and the physical size of the SQL server is around 80gb… however the VHD files are still over 500gb. My question is until this post, I always thought you were supposed to run the compact utility in HyperV? The other question is I have three physical files that make up my SQL VHD file ending with -1, -2, and -3…… do I run diskpart on each?

  2. Commented on
    Comment by Phil

    Pretty good information! Used it to reduce my VDA image size. (XenServer dynamic .VHD on the PVS for XenDesktop desktop)

  3. Commented on
    Comment by Juergen

    I had a data loss 3 days ago (S.M.A.R.T. error), so I dumped the major data partition with 100G with dd. Then I converted the disk with vboxmanage.exe to VHD, but this file showed as 99G although it was a dynamic format VHD.
    Now I followed your guide – it took about 15 minutes to compact my vdisk from 99G to 40G. thank you very much, useful information.

    regards, juergen

  4. Commented on
    Comment by Dshah

    Initially ran into an issue with the error of file system limitation. Found an article at

    Deleted all Shadow copies and then ran the diskpart as described above and Voila.. 80GB down to 19GB.. Thanks a bunch!!

  5. Commented on
    Comment by Andira Muttakim

    thx this is very useful

  6. Commented on
    Comment by Martin Kirk

    Remember to run Defrag and PreCompacter before compacting… Also, clean up the VHD by removing Temp files, RecycleBin and Windows Update Cache … will save you a multiple of GBs !!!

  7. Commented on
    Comment by Harry The Bustard

    When I tried the suggested file path (including name) format I was given “The pathname for a virtual disk must be fully qualified” – the solution to which was to add a backslash after the colon.

Comments are closed.