How to compress virtualbox container exported file, for example, vdi file

1. Run defrag in the guest (Windows only)

2. Nullify free space: 

With a Windows Guest, download SDelete from SysInternals and run this:

https://learn.microsoft.com/en-us/sysinternals/downloads/sdelete

sdelete.exe c: -z

(replace C: with the drive letter of the VDI)

3. Shutdown the guest VM

4. Now run VBoxManage’s modifyhd command with the --compact option:

Go to ~/VirtualBox VMs, get into your system container folder.

With a Linux Host run this:

vboxmanage modifyhd /path/to/thedisk.vdi --compact

With a Windows Host run this:

VBoxManage.exe modifyhd c:\path\to\thedisk.vdi --compact

With a Mac Host run this:

VBoxManage modifyhd /path/to/thedisk.vdi --compact