Shrink VirtualBox VDI Image
Virtual Box VDI images can get much relatively large over time. This is a brief guide to shrinking these images.
After an import of an image clone or after uninstalling large packages, clearing caches, ... There are many reasons to shrink a grown VDI image so that it consumes only the space that is actually occupied.
Shrinking VDI disks is only possible if the media is configured as "dynamically occupied" (See VirtualBox "Virtual Media Manager").
Procedure
Step 1: Delete all unnecessary files inside the VM (e.g. sudo apt-get clean).
Step 2: Fill disk with zeros and then delete the dummy file. This allows for later compression. Then shut down the VM.
sudo dd if=/dev/zero of=/var/dummy bs=8126k rm -rf /var/dummy
Step 3: Start compression on the host system. The following example is for macOS:
VBoxManage modifymedium disk myvmdist.vdi -compact