How to compact VirtualBox’s VDMK file size

This guide explains how to shrink (compact) the virtual disk files (files having extension .vmdk) of your VirtualBox virtual machines so they consume less disk space. It is also easier to upload and share them with other people.

Fill the free space with zeros
VirtualBox only knows that the space is really free if it’s been set to zero, and standard deletion won’t do this. Login into the virtual machine and run the following command:

cat /dev/zero > zero.fill; sync; sleep 1; sync; rm -f zero.fill

Navigate to VirtualBox virtual machine folder
The virtual machines you have configured in VirtualBox are stored in folder “VirtualBox VMs” inside your home directory (or at least when Ubuntu is your host machine). Every machine has its own directory with name starting with the machine name. You have to navigate to the folder containing the files of the virtual machine having disk you want to compact and open terminal.

Get the UUID of the virtual disk
You have to obtain the UUID of the disk you want to shrink. I will explain you why later.
The command to do this is:

vboxmanage showhdinfo box-disk1.vmdk

This command assumes that your working directory is the directory that contains the vmdk file. Of course, you have to change the name of the vmdk file if it is different.
Write down the reported UUID because you will need it later.

Convert it to vdi
In order to compact the disk, you need to convert it to VDI format using this command

VBoxManage clonehd box-disk1.vmdk box-disk1.vdi --format vdi


VBoxManage modifyhd box-disk1.vdi --compact

Convert it back to vmdk

VBoxManage clonehd box-disk1.vdi box-disk1.vmdk --format vmdk
rm box-disk1.vdi

Set the original UUID
You converted the original vmdk disk file to vdi, compacted it and created new vmdk file from the compacted one. So, basically, you have created new virtual disk with new UUID which will not be recognized by VirtualBox unless you set the original disk’s UUID to the newly created one using the following command:

vboxmanage internalcommands sethduuid ./box-disk1.vmdk <original UUID here>

Voilà! The virtual disk size is successfully reduced!

Leave me a comment if you have any troubles or ideas how to improve this article. I am not VirtualBox expert and so I can’t guarantee that the above guide is the easiest way to get the job done.

5 thoughts on “How to compact VirtualBox’s VDMK file size

  1. makcum

    thanks for the post. very helpful!

    One small thing:
    Convert it back to vmdk
    rm box-disk1.vdi

    should read rm box-disk1.vmdk otherwise you are deleting the image you\’ve just compacted 😉

  2. monica

    Make it simple, how about:

    – fill the free space with zeros
    – clone the vmdk

    That\’s it 🙂

  3. Geoff

    Worth noting that you have to have enough space to make a full copy of the virtual disk to be able to do this. In my case the reason I want to compact it is that I\’m running out of disk space and don\’t have enough room to make a copy. Wish VirtualBox could come up with a neater way of doing this without all the hassle.

  4. Anonymous

    What about if they are differencing files, how to compact/shrink the last ones on a big tree (nodes without descending nodes)?

    And no one mention a vmware comand-tool that does it on one pass, without need to convert, use extra space on host disk, etc?

    On windows host: vmware-vdiskmanager.exe -k \”full-path-to-file-vmdk\”
    On linux host: vmware-vdiskmanager -k \”full-path-to-file-vmdk\”

    Just check official web site, it is well documented:

Comments are closed.