Compact VirtualBox Hard Disk .vdi

By | 2011/05/09

If you use VirtualBox ‘Dynamically Expanding’ disk images, you can also compact the image back down in size.

This example shows how to shrink down a Windows virtual machine guest running on a Linux host.

Step 1:

Inside the Windows VM, defrag the hard drive. Go ahead and defrag even if it says a defrag is not necessary.

Programs > Accessories > System Tools > Disk Defragmenter

Step 2:

Inside the Windows VM, empty the free space to zeros with the sysinternals app sdelete.

sdelete.exe is available from:

Start -> Run -> cmd

cd to the location of sdelete, then run:

sdelete -c -z c:

Note: This may temporarily max out the disk size. We will compact it down in the next step.

After sdelete completes, completely shut down your Windows VM.

Step 3:

From your host, run the following command to compact your Windows VM:

$ VBoxManage modifyhd /fullpath/to/windowsdisk.vdi --compact

Note: You must specify the full path for the .vdi for this command to work!

With some VMs I saved 10GB of space! Check it out!