Reducing Size of VirtualBox Harddisks

VirtualBox is my favourite tool for virtualising my systems. To save diskspace on my physical machine I am using dynamic harddisks. In this case the VDI file is allocating necessary space when it needs it. Once the space was used it is not being freed in an automatic way. So you have to do this by hand. Just follow the steps below:

1. Start your virtual machine
2. Zero out free space inside your virtual machine. Under Windows you can do this with SDelete
Just run the following command with [driveletter] replaced by the drive (i.e E:) you want to free.

sdelete -c [driveletter]

3. After the command has completed shut down your virtual machine
4. Run the following command on your host. [vdifilename] has to be replaced by the full path of the VDI you want to compact.

VBoxManage modifyvdi [vdifilename] --compact

VBoxManage is located inside your VirtualBox installation folder.
Depending on the size of your VDI file the command can take several minutes to complete.