This post demonstrates bash zerofree
to shrink virtual box disk image
Our goal is to remove unused space from a virtual box disk image file (vdi)
Boot the recovery root console and list all partitions. Remount and zerofree the device which you wan’t to shrink:
replace /dev/sdx1
with your disk partition f.e. /dev/sda1
#!/bin/bash
fdisk -l
mount -r -o remount /dev/sdx1
zerofree -v /dev/sdx1
sync
shutdown -h now
VBoxManage modifymedium
VBoxManage.exe modifymedium disk "C:\path\to\mydisk.vdi" --compact