HomeHowTo › How To Reduce The Size Of VirtualBox VMs

How To Reduce The Size Of VirtualBox VMs

(Updated 2018-10-01)

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. 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 works on base images as well as differential images (snapshots).
  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.

Both approaches explained in detail:

 

A. VBoxManage modifymedium

Caution: I never had any trouble with compacting images, but since this is a critical process which might break the VM if something goes wrong, it might be good to have a backup.

See the official documentation for further information on the usage of this command.

1. Fill Free Space With Zeros

Boot up the VM and fill the free space with zeros.

1.1 Windows

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

1
2
3
4
sdelete -z

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

compact_vdi_01_windows_sdelete

 

 

 

 

 

1.2 Linux

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

1
2
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.

compact_vdi_02_linux_dd

 

 

 

 

 

 

2. Compact The VDI

1
VBoxManage modifymedium somedisk.vdi --compact

compact_vdi_03_modifyhd

 

 

 

 

 

B. Clone VM

1. Prepare The Machine

See A.1

2. Clone The Machine

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

VBoxManage clonevm "name_of_vm"  --name "new_name" --register

This will clone the current state into a new machine and add it to VirtualBox.

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