HowTo: Compact Virtualbox VDI images

I just ran into an issue while trying to compact my Virtualbox hard drive images. On virtual NTFS filesystems I usually run defrag twice, then rely on a tool called sdelete (download page) to zero the free space within the image. Afterwards I use vboxmanage to reduce the image size.

This time either zeroing the free space or the shrinking process seemed to fail. The supposedly compressed images needed even more space than before. It took me a while to figure out what happened – the parameters of sdelete had changed:

It feels like 99% of the tips concerning zeroing the free space (by calling sdelete -c, the old option for zeroing) seem to be outdated. Even worse: the “clean free space” feature shows the same output as “zero free space”.

So this should (currently) be a working procedure:

On the guest system: Run the Windows “defrag” tool twice.

On the guest system: (Download and) run “sdelete -z” to zero the free space.

On the host system: Use vboxmanage “modifyvdi IMAGE.vdi –compact” to reduce the image size.

This observation is correct: zeroing out the free space with sdelete initially leads to an increase in the size of the image. The second step (vboxmanage … –compact) identifies empty space (filled with zeroes) and removes it.