While using Virtualbox, one thing that I have found is that the size of the VM (a file with .VDI extension) expands over time. If you have selected the “Dynamically Expanding Storage” option when first creating the VM, you will find that the file size expands whenever you install new applications in the VM. That is perfectly normal. However, when you uninstall or delete applications in the VM, the file size does not shrink or return to its previous size. That is to say, if you are constantly installing and uninstalling applications in your VM, the VDI file could be taking up more space than it is actually using.
If you have a great deal of hard disk space to spare, you can simply ignore this. However, if you find yourself always running short of hard disk space, you might want to consider shrinking the VM and free up unused disk space.
This tutorial will go through the steps to shrink your Virtualbox VM.
It doesn’t matter if you are using Windows/Linux host. The instructions for a Windows and Linux guest are slightly different though. (I have not tried this on a Mac host yet. Theoretically it should work, but do it at your own risk.)
For Windows guest
If you are using Windows guest, do the following:
- Uninstall all unnecessary applications
- Perform a hard disk defragmentation. If you are using WinXP, you You can access the inbuilt disk fragmenter from Start -> All Programs -> Accessories -> System Tools -> Disk Defragmenter.
Download sdelete and extract it to your desktop.
Open a command prompt and cd to the sdelete directory. Type in the following command:
sdelete -z c:/
This will add zeroes to all the free space in your C: drive.
Once it is done, shutdown the VM.
For Linux guest
If you are using Linux guest (this tutorial assumes you are using Ubuntu and is running on ext2/ext3 filesystem. It does not work on ext4), here is what you need to do:
- Remove any unused applications.
- Clean up your Linux VM
Install zerofree
sudo apt-get zerofree
Restart and boot your Linux VM into Recovery mode. (This is generally the second option in the Grub boot up screen.)
In the root shell prompt, type
dfThis will display all the available filesystem in your OS. Record down the filesystem where the main directory lies. (it should be in /dev/sdaX or /dev/hdaX format)

Mount the filesystem in read-only mode with the following command:
mount -n -o remount,ro -t ext3 /dev/sda1 /
replacing the /dev/sda1 with the filesytem info that you have recorded just now.
Next, type the following command to zero out all the free space.
zerofree /dev/sda1
Once again, replace the /dev/sda1 with your own filesystem info.
Once you are done, shutdown the Linux VM:
shutdown -h nowShrinking the VM
Now that we are done with clearing up the VM and zero out all the free space, it’s time to shrink the file size using the VboxManage command.
If you are still using the older Virtualbox 2.0.x version, you can easily shrink the hard disk with the following command:
VboxManage modifyvdi /path/to/your/VM.vdi compact
If you have upgraded your Virtualbox to the 2.1.x version, you will find that the above command will no longer work. There is a bug in the software that disable the use of the modifyhd command. An alternative solution is to use the clonehd command to creat a smaller clone copy of the original VM.
In your terminal (or command prompt), type the following:
VboxManage clonehd name-of-original-vm.vdi name-of-clone-vm.vdi
This will create a clone of the original VM with the name that you have specified in the command above.
Result
After shrinking my VM, I manage to shave 45% off the size of the oirginal VM and free up a whopping 4.5GB of hard disk space to store my other stuff.
The original VDI: 10GB

The cloned VDI: 5.5GB

Importing the clone VM into Virtualbox
The whole process is not finished yet. You still need to import the clone VM into Virtualbox and remove/delete the original VM from your system.
In your Virtualbox console, click on the New button.
Follow the instructions to create a new VM. When it reaches the screen to ask you to choose the hard disk, select Existing.

In the Virtual media manager window, click Add to add the newly cloned VM.

Highlight the newly added VM and click Select.
Back to the Hard disk selection window, you should see your new VM in the dropdown selection. Click on Next to finish up the import.
Once everything is in place, run the newly added VM to make sure that it is working fine.
On the main Virtualbox screen, highlight the original VM and go to Machine -> Delete. This will remove the entry from the console.
Go to File -> Virtual Media Manager. Highlight the original VM and click the Remove button. When prompt if you want to keep the file, select Delete. This will delete the vdi file from your system.
That’s it.
Thank you for your writeup. I found your work-around for the missing compact feature very helpful. One thing to note is that using clonehd as a workaround changes the UUID. I have not found a command to manually revert the UUID for the purposes of making it a drop in replacement for the pre-shrunk VDI. Have you encountered a solution?
Great, but is does not work.
The clonehd command creates a new vdi which is exactly as large as the original vdi, 15.3 Giga Bytes.
However, windows (the vdi is a virtual XP system) reports that it
only uses 8.9 GiB.
Your thoughts please.
Eagleman
@Eagleman
clonehd did indeed shrink my image. I would recommend following all of the steps including defragging and zeroing out the empty space to ensure a successful shrink. The one drawback with this method is UUID change.
IT WORKS!
What I did not do (read the f** manual!) is running SDELETE in my XP guest OS.
CLONEHD actually shrunk my vdi from 15.3 to 9.3 GiB.
Thanks everybody!
Eagleman
Thanks Damien. I’m surprised how do you write a bunch of useful articles while on other blogs i read 1 needed article, then go away.
Regarding this article. Glad that command “VboxManage modifyvdi |—| compact” worked on my Intrepid with VirtualBox 2.2.2. However i faced one difficulty with it – “bash: VboxManage: command not found”.
The workaround was to change “VboxManage” to ‘/usr/lib/virtualbox/VBoxManage’ (path to executable with quotes), followed by arguments mentioned in article.
You are the best man…
I only need these command (virtualbox 2.2.4):
1. sdelete -c c:/
in windows
2. VboxManage modifyvdi HardDisks/Win.vdi compact
in my host..
cool..
Has anyone found a way to change the UUID yet? This being to fix the outcoming image from clone performed while the modifyvdi was disabled in the 2.x versions before current.
I don’t suppose there is a method for Mac OSX users is there…?
Theoretically, the method is the same for Mac OSX users, but I haven’t give it a test yet, so I can’t confirm whether the above method works in OSX or not.
I will update again as soon as I have tested it out. :)
The command “VBoxManage modifyvdi /path/to/your/VM.vdi compact” works with version 2.2.4. Just make sure you type VBoxManage instead of VboxManage.
the smae command should work on a Mac host. except has slightly renamed switches. just open up a terminal windows and run the command.
VboxManage modifyhd /path/to/your/VM.vdi –compact
similar command works with Ubuntu 9.04 host using VBox 3.0.4.
VBoxManage modifyhd –compact /path/to/your/VM.vdi
Great and useful tutorial. Thank You. I was able to get the VboxManage modifyvdi command (with lowercase b) to work fine on Mac OS X Leopard with VB 2.2.4 http://bit.ly/yHk1t
In Linux version of VirtualBox 3.x, the command is VBoxManage instead VboxManage (note the uppercase B)
I'm having trouble with the remount. I type
mount -n -o remount,ro -t ext3 /dev/sda1 /
and I get back
mount: / is busy
what am i doing wrong? Another question, how do I know if I should use ext2 or ext3?
it's likely user error, as I am new to linux, but either the
mount -n -o remount,ro -t ext3 /dev/sda1 /
or zerofree utlity completely screwed up my ext3 files system. The system boots, but everytime I try to create a file more than a few MB, it freezes, and is unable to shutdown. It errors saying something about how the filesystem is read only, but the OS level utilities tells me the two filesystems are read-write. It completely toasted an image I was working on for 2 days and had to rebuild from scratch.
A colleague suggested I skip the whole complex boot from iso process as you describe, and simply do a
dd if=/dev/zero of=/bigemptyfile
where /<your mount point-filesystem>/bigemptyfile will create a file of zeros as large as the available free space in your virtual HD. Then delete the file, shutdown the guest OS, run the vboxmanage compact utility, and export the appliance. I got a 10GB worth of virtual disk to shrink to 2.5 GB that way. Much less risky than the “boot in recovery mode” procedure that you describe.
I think your method is much better than mine. This method didn't come across my mind until you mention it now. Thanks for sharing.