Compact VirtualBox VM .vdi Disk Size

NOTE: This process only works on dynamically allocated virtual disks. If you're not sure, launch VirtualBox and go to File > Virtual Media Manager. Select the virtual drive and view the properties

Create backups of virtual disks before attempting these steps

Compacting Windows Guest VM

  1. Launch the Windows VM
  2. Inside the VM, launch a web browser and download the sdelete utility Download
  3. Extract the downloaded sdelete .zip file
  4. Copy sdelete.exe for 32-bit OS or sdelete64.exe for 64-bit OS to the C:\Windows directory
  5. Launch Command Prompt by right clicking the Start button > Run > type cmd > Press Enter
  6. Run the following command to zero out the free space on the disk, replace the drive letter to target additional drives
    sdelete64.exe c: -z
  7. Once the process completes, shutdown the VM

Compacting Linux Guest VM

  1. Download the SystemRescue live disc Download
  2. Launch VirtualBox Manager
  3. Right click the VM to be compacted > Settings
  4. Select Storage on the left hand side
  5. Select the optical disc drive
  6. Click the disk selection icon > Choose a disk file...
  7. Navigate to the downloaded SystemRescue .iso file
  8. Click Open and OK to close the settings
  9. Right click the VM > Start > Normal Start
  10. Press Enter to boot SystemRescue using default options
  11. Once SystemRescue has finished booting, run the following commands
    # list the attached disks
    fdisk -l
    # find the disk to be compacted
    # usually listed as /dev/mapper/....
    # run the following, replacing the drive identifier as needed
    zerofree -v /dev/mapper/
    # after zerofree completes, shutdown the VM
    shutdown now
  12. Go back into the VM settings and remove the attached SystemRescue .iso

Compacting the Virtual Disk

  1. Launch VirtualBox Manager on the host device
  2. Right click the VM to be compacted > Show in Explorer
  3. Right click in the white space > Open PowerShell window here...
  4. Run the following command, replacing the file name of the virtual disk
    "%programfiles%\Oracle\VirtualBox\vboxmanage.exe" modifymedium disk "name_of_disk.vdi" --compact