Compacting (reducing maximum size of) VDI files for VirtualBox

A large maximum dynamic disk size for a VirtualBox guest is problematic. Even if one uses a small portion of that maximum size, the guest OS (especially Windows) will spread itself out. A 10 GB Windows image will climb to the maximum size of the dynamic VDI file over time (say to 50 GB).

Here’s a fairly painless procedure to fix this, taking less than 1 hour on a modern PC. I assume your guest OS is Windows, and your host OS is Linux running VirtualBox.


  1. in guest Windows, run Disk Defragmenter
  2. in guest Windows, in Command Prompt, run

    sdelete -z c:
  3. shutdown guest Windows, and “insert” the Gparted ISO file. You do this by right-clicking the CD icon at the bottom of the guest OS VirtualBox window and selecting your ISO file.
  4. Resize your guest Windows partition so that you have only say 8 GB free (e.g. original partition 40 GB, only 10 GB used –> resize to 18 GB)
  5. reboot guest Windows, removing the Gparted ISO file from VirtualBox. You will see Windows automatically runs CheckDisk–this is normal. You will need to reboot guest Windows once more, then shutdown guest Windows
  6. in host Linux, in Terminal type

    cd ~/VirtualBox*/WinXP (or wherever your .VDI file is located. Then type in Terminal
    vboxmanage modifyhd --compact MyFile.vdi where MyFile.vdi is the name of your VDI file.
  7. Now you’ll see in Linux that the size of your VDI file has dropped a lot!

Note: for future reference, when you Defragment in guest Windows, this actually makes your host VDI file INCREASE in size by the amount of bytes that were moved to defragment!

To fix this, repeat steps 1, 2, and 6 of the procedure above when you find your VDI has grown to an annoying size.

The take away point is, do not make excessively large maximum size Dynamic VDIs to start–this problem will only repeat itself over time.

Leave a Comment