HomeHowTo › How To Reduce The Size Of VirtualBox VMs

How To Reduce The Size Of VirtualBox VMs

If you work with dynamically sized disk images in VirtualBox, you will notice that the image size increases over time as you’re working with the VM, but also that the size will not decrease if you delete files inside it. This has two reasons:

  • Just like on physical disks, when you delete a file, it is not actually deleted. The operating system just “removes it from the index” and marks the corresponding blocks as free.
  • Every disk block that contained data before will remain in the virtual disk image even if the block is now empty (zeros). So if you securely delete a file or fill the free space with zeros, the image size will not automatically decrease.

But VirtualBox has features to manually reduce the image size. These however require prior zeroing of the free space inside the VM with a third-party tool. Additionally, these are only effective if the VM does not have any snapshots, which means that you’ll have to delete all snapshots or clone a new VM from the desired state. Then there are two approaches to reduce the image size:

  1. Using the command-line utility VBoxManage you can execute a command to remove free space (zeros) from the disk image. This only works with VDI files at this time.
  2. Alternatively you can clone the VM after zeroing the free space. Doing so, only the blocks that actually contain data will be copied into the new machine/image.

Caution: If the VM has snapshots, zeroing the free space leads to the current differential image getting very large (–> as large as the virtual disk size).

Both approaches explained in detail:


A. ModifyHD

1. Merge All Snapshots or Clone

Restore the machine’s state that you want to keep and then delete all snapshots, so that you end up with just the base VDI containing your current state. Caution: Deleting snapshots is a critical process that might break the VM if something goes wrong, so I recommend to have a recent backup, just in case. Alternatively you can clone the desired state into a new machine.

2. Fill Free Space With Zeros

Now boot up your VM and fill the free space with zeros.

2.1 Windows

Get the sdelete utility from Sysinternals and run it as follows:

sdelete -z

# Update: it might be necessary to specify a drive letter:
sdelete -z c:







2.2 Linux

You can use a utility like zerofree but you can also do it with dd:

sudo dd if=/dev/zero of=/emptyfile bs=1M
sudo rm -rf /emptyfile

This will create one big file that only contains zeros. The file will get as big as the free space was before, so dd will abort when there is no free space left. Then you just delete the file.








3. Compact The VDI

VBoxManage modifyhd somedisk.vdi –compact







B. CloneVM

1. Prepare The Machine

See A.1 and A.2

2. Clone The Machine

Use the Virtual Box GUI to clone the current state into a new VM or use the following VBoxManage command:

[cce_bash line_numbers=”false”]
VBoxManage clonevm “name”  –name “new_name” –register

This will clone the current state into a new machine and add it to VirtualBox. You could also use the clonehd command, but then you would have to deal with filenames and UUIDs.

11 Comments.[ Leave a comment ]

  1. Thank you,

    I was building a Windows VM for deployment in a lab and the virtual disk had been fully allocated after Windows Updates software installs and SysPrep.

    Following this guide took a 100 GB virtual disk and got it back down to ~30 GB. That’s going to save a lot of time pushing this out to 40+ machines.

  2. If you want to learn more about Snapshot and Clone in VirtualBox you can checkout VirtualBox Snapshot and Clone video tutorial.

  3. thank you. I was able to use these instructions successfully.
    Instead of using “vboxmanage clonevm”, I used “vboxmanage modifyvdi xxxxxx.vdi –compact”.

  4. On Linux you can use zerofree to zero the free space, this works in a way that is equivalent to sdelete on Windows. It should be faster and give better results than creating a large zero filled file and deleting it.

  5. “sdelete -z c:” doesn’t work in Windows XP Home Edition, it won’t do it to C drive, says it has to be d: or the likes, in other words, like it will only do it to a different partition. –compact appears to work, but ended up with same size as started with, –resize doesn’t work for shrinking a vdi, gives error that: “Resize medium operation for this format is not implemented yet!”
    I’m assuming –compact didn’t work because I had a 320GB dynamic vdi, with 80GB partition with WinXP on it, the rest was unallocated free space, and because evidently the free space on the partition and unallocated section wasn’t zeroed?

  6. edit: the correct command is “sdelete -z”. Once its done, it doesn’t matter, you can resize the partition in VBox but you cannot resize the logical drive, even after cloning, when you go to use it for a new machine, it will report the original machine size with the vdi as being the used portion of it. VirtualBox does not allow for shrinking the machine (anymore than you can clone a large disk to a smaller disk). If you try to make a new machine with the cloned vdi, it will tell you the vdi is used by another, if you use the cloned vdi as “existing disk” in the new machine, it will report the original machine size. The only real recourse is to uninstall (if Windows), create a new machine with the smaller size desired, and then install the OS in it. Linux a little quicker because you just delete the whole enchilada and start over.

  7. Thanks, this really worked.

  8. “sdelete -z c:” has been setting there for hours at 100% 😛
    Disk writes are pegged at 11MB/sec

  9. The virtual size is still the same as the original 🙁

  10. This approach reduces the actual size of the disk image file that resides on the physical system. From the VM’s point of view, the disk size remains the same.

Leave a Comment