Note that disk images can only be compacted if they are not in a snapshot chain and do not use disk encryption!
libVirt
How to compact a libVirt disk image
Link to original
VirtualBox
Windows
How to compact a VirtualBox Windows guest disk image
Disable the paging file (Settings → System → About → Advanced system settings → Advanced → Performance → Settings… → Advanced → Virtual memory → Change…), clean all temporary files (C: → Properties → Disk Cleanup), defrag (
defrag C: /FreespaceConsolidate /Verbose
), and then zero out free space with SDelete (sdelete -z C:
).Then, compact the disk image on the host:
Remember to turn the paging file back on after compacting the drive!
Link to original
Linux
How to compact a VirtualBox Linux guest disk image
Zero out free disk space on the guest:
Compact the disk image on the host:
Link to original