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!

9 thoughts on “Compact VirtualBox Hard Disk .vdi

  1. Albert

    Very nice tutorial, works flawlessly. Also congratulations for your blog design and usability.


  2. Todd Lefkowitz


    I followed your directions and was able to defrag and use sdelete without problems. My XP VM is located in the desktop of my MacBook Pro (it is called Clone of Windows XP 5-0.vdi). It was created from a .vmdk VM using CloneVDI. No matter how I phrase the command in my Terminal, I continue to get error messages, such as “could not find file for the medium…”

    Any ideas

    1. scott Post author

      Make sure you specify the full path to the .vdi – using quotes around it if needed.

      “/Users/username/Desktop/my xp image.vdi”

      Even if your current working directory has the .vdi in question, it still needs the full path.


  3. Todd Lefkowitz


    Thanks for taking the time to explain…I am still a noob to Linux and your patient explanation helped. It is finally working now..after hours of fruitless keyboard entry


  4. Larry

    You can reclaim even more space on Linux VMs by zeroing swap space before compacting.

    You must be root to do this. Depending on your distro, that’s:

    $ su
    Password: # root's password


    $ sudo -i
    [your] password:

    Next, list your swapfile(s). For example:

    # swapon -s
    Filename Type Size Used Priority
    /dev/sdx2 partition 1951888 0 0

    I used a very unlikely device name so that you don’t accidentally copy-and-paste something disastrous. OK, for each swapfile in the list, fill it with zeroes. In the example above:

    # swapoff /dev/sdx2
    # dd if=/dev/zero of=/dev/sdx2

    Shutdown right away and compact your .vdi

    A Windows version of this would involve disabling the page file before running sdelete. I’ll leave the details for another commenter, since I don’t have a Windows VM to practice on 🙂

  5. Larry

    Oops. Zeroing the swapfile partition blows away the swap file structure.

    One more thing to do before you shutdown, continuing the example:
    # mkswap /dev/sdx2

    (Sorry, have trouble getting the code tag work....)

  6. Simon Greenaway

    sdelete no longer wants the colon after the disk identifier, and can only support either -z or -c option but not both!

    This worked for me:

    sdelete -z c

  7. Michelle Tomyn

    Thank you so much for this simple, definitive list of steps!


Leave a Reply

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

Notify me of followup comments via e-mail. You can also subscribe without commenting.