Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I've a VirtualBox VM which configured a very large hard disk size (bigger than host). By my mistake, a program on the VM generated lots of log files and the VDI file size keeps growing until there is no space on the host.

Now I've deleted the log files but the VDI file size are not getting smaller after using VBoxManage.exe modifyhd "C:\Virts\mybox-i386.vdi" compact

Is there a way to really compact the VDI file size? Thanks!

share|improve this question

3 Answers 3

up vote 101 down vote accepted

You have to do the following steps:

  1. Run defrag in the guest (Windows only)
  2. Nullify free space:

    With a Linux Guest run this:

    sudo dd if=/dev/zero of=/bigemptyfile bs=4096k
    sudo rm -rf /bigemptyfile

    With a Windows Guest, download SDelete from Sysinternals and run this:

    sdelete –z
  3. Shutdown the guest VM

  4. Now run VBoxManage's modifyhd command with the --compact option:

    With a Linux Host run this:

    vboxmanage modifyhd /path/to/thedisk.vdi --compact

    With a Windows Host run this:

    VBoxManage.exe modifyhd c:\path\to\thedisk.vdi --compact

This reduces the vdi size.

share|improve this answer
@user248749 FYI, dd with no argument copies one block at a time, not one byte. Using a larger block size can speed things up, but the effect is pretty minor. –  Gilles Aug 27 '13 at 0:15
For the next person, my command ended up looking like this: "C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" modifyhd "C:\Users\daniel\VirtualBox VMs\....\thedisk.vdi" --compact –  Daniel Dec 13 '13 at 17:57
According to the zerofree Linux utility manpage (manpages.ubuntu.com/manpages/natty/man8/zerofree.8.html), zerofree should be better than dd for this job. dd would not been recommended because "it is slow", "it makes the disk image (temporarily) grow to its maximal extent", "it (temporarily) uses all free space on the disk, so other concurrent write actions may fail". Zerofree is available on Ubuntu Linux via apt, or you can compile it yourself. –  Dakatine Feb 20 '14 at 13:16
It's fun that the the manpage of zerofree states that with dd other concurrent writes will fail, but zerofree needs the filesystem to be mounted read-only! *duh* –  Madarco Feb 25 '14 at 12:04
@Dakatine Using VirtualBox 4.3.10, the disk image file did not grow to its maximal extent. VirtualBox is clever enough to not bother writing all zero blocks to the physical disk. –  jlh May 14 '14 at 17:50

If running the command in the accepted answer produces an unhelpful error message like this

VBoxManage.exe: error: Cannot register the hard disk 'thedisk.vdi'
{aaaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeeee} because a hard disk 'thedisk.vdi'
with UUID {aaaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeeee} already exists

Simply run the command by the UUID instead of the filename:

VBoxManage.exe modifyhd {aaaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeeee} --compact
share|improve this answer

I'm on a Windows 7 host with Windows guests, Here is a batch file I wrote to Compact all of the VDIs in a folder tree

echo off
mode con:cols=140 lines=200
:: see https://forums.virtualbox.org/viewtopic.php?p=29272#p29272
:: How can I reduce the size of a dynamic VDI on disk?
:: but that page says to use sdelete -s which is suboptimal. 
:: use -z as per http://technet.microsoft.com/en-us/sysinternals/bb897443.aspx

:: First run the sdelete -z c: inside the VMs that zero-out all the free space
:: THEN run this batch file 

Title Compacting Free space on Virtual Machine VMs

:: http://ss64.com/nt/for_r.html
:: http://stackoverflow.com/questions/8836368/windows-batch-file-how-to-loop-through-files-in-a-directory/8836401#8836401

Setlocal EnableDelayedExpansion
:: http://ss64.com/nt/delayedexpansion.html ... 
:: Notice that within the for loop we use !variable! instead of %variable%.

For /R %CD% %%G IN (*.vdi) DO (
 set ohai=%%G
 set lastfive=!ohai:~-5!
:: Skip snapshots which are named {guid}.vdi
 if NOT !lastfive!==}.vdi (
 echo .
 echo Compacting %%G
 "C:\Program Files\Oracle\VirtualBox\VboxManage.exe" modifyhd "%%G" --compact )


I left the links in the comments so you can (sort of) tell how it works.


Well, after all that, I tried the CloneVDI tool and it did a good job in much less time and in one click.

share|improve this answer
You would think on this sort of site there would be some kind of syntax highlighting for DOS but no. It looks much prettier in Notepad++ –  CAD bloke Jan 9 at 10:26

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.