How to compact your Virtual Disks

In the previous article, we've discussed the reasons that cause your virtual disk images to become quite large over time. It might seem to be a pretty hopeless situation, but let me assure you: there is a way out. This article will show you how to reclaim your disk space. A couple of steps are presented and while each of them make sense, it is important that you execute them in the exact order given if you want to achieve the best results.

The first step (don't shoot me for that) is to delete files in your VM you don't really need. Clean up your trashcan, the temp directories, download stuff, uninstall applications you never use, etc. There are a lot of tools out there to delete unused files from Windows, I'm sure you will find a tool that suits your needs.

Now when you have just the files on the disk that you need, it's time to defragment the hard disk. After defragmentation, the files will be nicely aligned and there will be little free space between files. Windows comes with (not so good) defragmentation software, you will find it in the properties of your disk drive in My Computer under the "Tools" tab. Just give it a run and hope it will improve fragmentation. There are better tools out there, some of them cost money.

The data is now nicely aligned but we still got all those unused blocks that contain garbage (the contents of the files that used to live there). Therefore we need a tool that can find these blocks and overwrite them with zeros. Windows does not come with such a compact tool but it's available for download from Microsoft:

Close all your programs and do a

sdelete.exe -c

This will take a while, you should let it do its job without interfering. The tool will go through all parts of your virtual disk and look for things it can wipe out. It's known to be a very safe process, so don't worry.

Next, you should shutdown your virtual machine (power off, not save state) and let VirtualBox optimize the disk image and cut out all parts that SDelete zeroed out. There are two ways to do this: first you could compact the image (this will just operate on the disk image and make it smaller) or you could clone the disk image to a new image. The former needs more disk space but the latter has the advantage of being more secure (you still got the orginal bloated file after all) and it even allows you to switch from one virtual disk format (e.g. VDI) to another (e.g. VMDK). Let look at both options.

VBoxManage modifyhd XP.vdi --compact

This will compact your disk image and it will take some time. The cloning from VDI to VMDK works as follows:

VBoxManage clonehd XP.vdi NewXP.vmdk --format VMDK 

There are a lot more options to clonehd and modifyhd, have a look at the VirtualBox user manual.

This concludes our article and I hope I've given you some useful information that allows you to reclaim some of your disk space. I'm about to go on a 11h intercontinental flight and the virtual machine I want to work with was too big for my small notebook so I've used these techniques to shrink the image to less than half of its previous size. 


Are there similar tools for the file systems on ubuntu and Solaris

Posted by Louis Vuitton bags on January 27, 2010 at 02:15 AM CET #

We offer <b><a href=>Louis Vuitton bags</a></b> here you <b><a href=>Louis Vuitton replica bags</a></b>

Posted by Louis Vuitton bags on January 27, 2010 at 06:38 PM CET #

Thank you very much for this useful information.

Posted by Ufuk Guler on May 23, 2010 at 12:20 AM CEST #

I found this page looking for help with vmware server virtual disks. If it doesn't offend VirtualBox enthusiasts, I'll add how to do it with vmware server to help those in my situation.

All of the above works for vmware server images, with the exception of replacing

VBoxManage modifyhd XP.vdi --compact


vmware-vdiskmanager -k diskname.vmdk

On the plus side, finding this page inspired me to try VirtualBox. Its a very nice alternative to Vmware Workstation.

Posted by Matthew on June 08, 2010 at 08:44 PM CEST #

Molto utile GRAZIE!

Posted by byrollo on April 22, 2011 at 06:58 AM CEST #

Hello guys. And what if the disk name contains space chars? i.e. "Win 2000 Pro SP4". How must be called the params for VBoxManage.exe?

Posted by Marcelo on May 17, 2011 at 09:21 AM CEST #

linux alternative: cat /dev/zero > zero.fill;sync;sleep 1;sync;rm -f zero.fill

Posted by guest on May 26, 2011 at 08:10 AM CEST #

Tried this using VBox 4.0.12, and it's done nothing to actually compact the disk image, which is now using it's full 32gb allocation despite only 10gb being used inside the image! (and yes, I remembered to do defrag followed by sdelete -c to zero out the unused blocks)

Any thoughts? Is --compact broken in later versions of virtualbox using vdi images?

Posted by guest on September 26, 2011 at 04:59 AM CEST #

Turns out my issue was due to the meanings of sdelete's command line arguments changing. sdelete -z is what should be used now, not -c

Posted by anomaly256 on September 26, 2011 at 05:04 AM CEST #

I downloaded sdelete.exe v1.6 a few days ago. Using sdelete -c yields nothing when using the modifyhd --compact or clonehd utilities. In fact, it has the opposite effect because it grows the thin provisioned virtual hard drives to their maximum size. It also takes a very long time to run.
Even though the online documentation has not been updated, sdelete -h reveals that the -c switch is for "clean", and the -z switch is for "zero free space"
With sdelete v1.6, use "sdelete -z c" to prepare your virtual C: drive for a compact or clone operation.

Posted by guest on September 26, 2011 at 10:03 AM CEST #

The current version of SDelete (1.60) has swapped the meaning of the -c and -z flags.
I ran sdelete -c and was a bit stunned when my vdi file became larger so I ran sdelete -h and realized I should have used -z instead.

Posted by guest on October 19, 2011 at 09:54 AM CEST #

Post a Comment:
  • HTML Syntax: NOT allowed

This blog concerns all things VirtualBox.


« July 2016