How to compact VirtualBox’s VDI file size

bimboingabbiaIf in creating process of Virtual Box VM you have selected the option Dynamically Expanding Storage, you will find that the file size expands whenever you install new applications in the VM or add new files.
However, when you uninstall or delete applications, the file size of the VM does not shrink: if you are constantly installing and uninstalling applications in your VM, the VDI file could be taking up more space than it is actually using.
In this post I?ll show the steps to shrink your Virtualbox VM.

Windows Guest
Inside Windows guest.
– Uninstall all unnecessary applications
– Perform a complete hard disk defragmentation.
– Download sdelete (see linkografia) and extract it to your desktop. This software zeroes the not used sectors on disk.
– Open a command prompt and cd to the sdelete directory. Type in the following command:

sdelete -z c:/

– Repeat for all other drive
– Shutdown the VM

Linux Guest
Inside Linux Guest
– Uninstall all unnecessary applications
– With administrative rights in command prompt launch next command

dd if=/dev/zero of=zerofillfile bs=1M

This command will zero-fill any free disk space on the virtual Linux drive creating a new file.
– Once the dd has completed, you will see a message in your terminal window indicating that there is no space left on the device: now you can remove zerofillfile using the Linux command rm.

rm zerofillfile

– Shutdown the VM

Shrinking the VM
Inside the Host

VboxManage modifyvdi /path/to/your/VM.vdi compact

How To Shrink Your Virtualbox VM And Free Up Space For Your Hard Disk
Compacting VirtualBox Disk Images – Linux Guests