When you create Virtual Machines there are a few options for the virtual hard disk file type and it's storage. Besides the different formats you can choose between dynamically allocated or fixed size files.
VirtualBox has some very powerful command-line tools to deal with different aspects of managing virtual machines. Shrinking and recovering disk space on dynamic disk files is one of those tasks that you do from the command line. You can either use Vboxmanage that comes with Virtualbox or you can use a 3rd party tool. Both methods described below.
Many of us runs Virtualbox and multiple VM's on our laptops. We don't have unlimited storage. Compacting Virtual Disk files can save GB's
It is now possible to export / upload your Virtualbox VM's to OCI. Compacting your VM's disk file will save bandwidth and time when uploading to OCI.
Before you can shrink a VM disk image file you need to zero out all unused disk blocks. So for every file system (this is for linux file systems) in the guest you need to do something like this (as root):
dd if=/dev/zero of=zero.tmp bs=10M; rm -f zero.tmp
There are a couple of alternative methods and tools available but they all basically do the same thing. Write zero's to unused blocks.
For Windows there is a tool called sdelete (secure delete?) that will do this.
Stop the VM and run the command below on the command line for every disk file you have.
VBoxManage modifyhd -compact thediskfile.vdi
This also only works on VDI files. If your VM has any other disk image format such as vmdk you can use the Vboxmanage clone feature.
VBoxManage clonehd myoldfile.vmdk mynewfile.vdi --format VDI
There is a free tool called CloneVDI that you can download and use. It's been around for more than 10 years. It’s a Windows tool but as you can see from the screenshot below I use it with Wine (WineBottler is a nice packaging of Wine on macOS, I use the latest development builds) on my mac. You can also install Wine via Homebrew.
Wine and CloneVDI seems to not work on macOS Catalina. Likely due to CloneVDI being a 32 bit app.
This tool will read most any virtual disk format (Reads VDI, VHD, VHDX, VMDK, Parallels HDD, RAW files and physical drives, writes VDI.). I’m just highlighting two features in the image above but there is more to read here. CloneVDI has knowledge about the file system in the VM so in the cloning process it will only write used data blocks to the new VDI file (you need to check the checkbox shown above of course).
You still need to zero out unused blocks though, see above.
The process of writing a new disk image file is very fast and stable. I have used CloneVDI for years and it has never produced a corrupt output. It also never touches the source disk file so I consider this a safe method. This is my preferred method.
When you clone a file with the settings as shown above you will get a new and smaller disk image file in the same folder as the original. Since it has the same UUID you can simply move or rename the original file and rename the clone to have the name of the original file. Virtualbox will not notice that you replaced the disk image file. Do not delete the original disk file until you verified that the clone works. I have never seen the clone not work but you can't be to cautious with destructive commands...
These are the settings I used with WinBottler to build a self-contained app. This app will be huge (+1GB) as it contains all the Wine libraries but you don't need to worry about dependencies.
Document links to Virtualbox command line usage above