If you have a VM using dynamically allocated disks, you may have the need to compact them to save some free space.
It happened that I have to shrink a Win10 VM after the upgrade to 1809, hosted on my Windows 10 notebook.
Using Virtualbox for Windows, actually, this option seems to be available only for VDI files.
The first step is to convert the disk files into VDI format.
You accomplish the task by using the following command line:
<virtualbox folder>\VBoxManage.exe clonemedium –format vdi disk <source disk full filename> <new VDI full filename>
A first solution to this task, is to use defrag, sdelete, and another VBoxManage command.
There are some problems to consider.
The first one is that we cannot use defrag on VDIs hosted into an SSD drive, because it will affect the lifetime of the device itself. The good news is that it is not mandatory if we don’t plan to shrink a VM disk partition.
The second is that using sdelete (from MS SysInternal) will expand VDI file size to its limit. This can be a problem if we don’t have enough free physical space.
I found a more convenient solution: the CloneVDI utility.
You can get it from VirtualBox forum:
This small program can do the work in one step only without expanding anything.
Set the source & destination filenames, check the compact option, and press the ‘Proceed’ button.