How to shrink a VirtualBox vdi disk image size?

Virtual disk images (.vdi files) tend to grow over time, especially with windows guest systems, and may quickly consume all your disk space on the host system.

Screenshot Virtual Disk Image Manager

This post shows the required steps to free up the unused space. It worked for me with Sun/Oracle VirtualBox versions of 3.1.6 and above.

For a Windows guest system

  • Start a Defragmentation (right-click disk icon, select properties->Tools tab-> Defragment now…)
  • Download SDelete (about 47kb) into your guest.
    Wait for the defragmentation to complete.
  • Fill the free space of your vdi disk image with zeros executing
    (with SDelete 1.5) sdelete -c or
    (with SDelete 1.6) sdelete -z
    from a DOS command line. (Check your version by entering only sdelete the option which is ‘good for virtual disk optimization’ is the right one)
    This step may take a lot of time (20min for a 20GB disk on my system). Don’t use the guest system during the process.
  • Shut down your windows guest system.
  • Make a backup of your .vdi file.
  • On your HOST system, execute the following command in a DOS/terminal window:

    VBoxManage modifyhd <diskname.vdi> --compact

    The VBoxManage tool is part of the VirtualBox standard distribution.

    This works only for .vdi files. If you want to shrink a .vdmk file, use the
    VBoxManage clonehd <in.vdmk> <out.vdi> --format VDI
    command.

Enjoy!

4 Comments

  1. Skip sdelete.exe step in case your VDI image is “expandable”.

    “Expandable” means that VirtualBox will allocate physical HDD space on your Host only when Guest requests to use that space. Since sdelete.exe writes zeroes to free disk space, it will actually grow VDI to its maximal allowed size.

    Martin

    Reply
    • Martin,
      SDelete is smart enough not to fill all empty space on a dynamic sized disk. I think you have been fooled by the horrible option change between Sdelete 1.5 and 1.6. the -c and -z options are inverted.
      When executing a ‘clean’ of the disk, this will efectively fill up your dynamic disk to it’s maximum size. Use the Zero Free Space option, whatever it is called in your SDelete version!

      Reply
  2. I just wanted to thank you for your article. The option change between Sdelete 1.5 and 1.6 is truly horrible and your article helped me understand why sdelete -c was filling up my dynamic disk !

    Reply
  3. Thanks, it works just fine! It decrease my Win Xp .vdi image from 4.2gb to 2.2gb.

    Reply

Submit a Comment

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>