Reducing the size of a VirtualBox hard drive file

VirtualBox hard drive files (*.vdi) can be created as dynamic files which expand to the requirement of the guest operating system. This is wonderful but often they do not represent the smallest size they could be; indeed they are often quite large with respect to their nominal contents.

To solve this, they need to be compacted. This is a relatively simple procedure and allows for the file size on the host machine to decrease to match the minimum requirements of the guest machine. In my situation, I have a Linux host and a Windows guest. To compact the vdi file, follow these three steps:

  1. Begin by deleting unnecessary files in the guest OS such as temporary files, internet caches etc. Then defragment the hard drive, making sure to consolidate all free space. I recommend UltraDefrag for Windows. Use the Full Optimisation setting and enable the Boot Time Scan then reboot for the best results.

  2. Once the defragment is complete, download and install sdelete from Microsoft. Run sdelete (e.g. sdelete -c c:\) and wait for it to zero out all the free space.

  3. After the zeroing is complete, shutdown the virtual machine and in the host operating system run vboxmanage modifyhd disk_file.vdi --compact. This step should be the same for all host operating systems.

That's it. The vdi file should now be significantly smaller. In my case, it saved around 15GB of space.

Published on 13th February 2012.