Defrag and compact .vdi images

If you have a lot of Virtualbox images (.vdi) disk space starts to become an issue (dynamically allocated images). So to reduce the size of the images there is a couple of tools we can use, and scripting to get the job done faster.

You might be familiar with "VBoxManage.exe modifyvdi my_vm.vdi --compact", this command and arguments will re-compact a .vdi image. But the way it works is by finding the end of data in the .vdi image file and saying from here an onwards is dynamic. Unfortunatly junk data (deleted files) in the image would still be considered as being data because it's not "empty".

This article will focused around running a Windows based operating system inside VirtualBox.

Tools we are gone need

To clear the junk data after defragmenting we are gone use the tool SDelete v1.61 by Mark Russinovich.

Visit/Download at

And we are gone use a different defragmenting tool: JkDefrag 3.36 by Jeroen Kessels. (Note that the latest version is now called MyDefrag, but because of the simple command line options we will use JkDefrag.)

Visit/Download at

(VBoxManage.exe comes with VirtualBox)

Compacting .vdi images on the host system

Let's start with a simple batch script to compact our .vdi files. The script below will use the command mentioned above to compact all the images in the same directory as it resides (You do not need to alter the script).

Put this script where your VirtualBox .vdi files are stored, and you run the script standalone whenever you need to compact the image(s) in that folder.


SET VBOXMANAGE=%ProgramFiles%\Oracle\VirtualBox\VBoxManage.exe
ECHO Compacting VirtualBox .vdi image(s) using VBoxManage...
FOR /f "delims=." %%a in ('dir *.vdi /b') do "%VBOXMANAGE%" modifyvdi "%%a.vdi" --compact

If you have many images, and want compact all images in one go then add the script below to the parent directory. It will simply run thru all the folders and call compact_vdi_images.bat if it exists.


FOR /d %%i IN (*.*) DO (
  IF EXIST "%%i\compact_vdi_images.bat" (
     CD "%%i"
     ECHO Processing: %%i
     CMD /C compact_vdi_images.bat
     CD ..

Your directory structure should end up look something like this:

Running the compact_all_vdi_images.bat script is gone take some time if you have a lot of images, or big ones.

C:\Documents and Settings\example\VirtualBox VMs>compact_all_vdi_images.bat
Processing: windows_xp_sp1_test
Compacting VirtualBox .vdi image(s) using VBoxManage...
Processing: win_xp_legacy_games
Compacting VirtualBox .vdi image(s) using VBoxManage...
Processing: my_cool_machine
Compacting VirtualBox .vdi image(s) using VBoxManage...
Processing: win_xp_sp3_test
Compacting VirtualBox .vdi image(s) using VBoxManage...

C:\Documents and Settings\example\VirtualBox VMs>

Defragmenting and zeroing empty space

In order to get a higher compact ratio we need to defragment, and zero the empty data on the virtual machine before compact them on the host machine.

We start by downloading JkDefrag and SDelete, and create a folder on the VM called c:\scripts. Extract the downloaded zip files in here (including folder names).

And we create a small batch script, defrag_drive_and_optimze.bat and place it in the same folder. JkDefrag will run with "fast defrag and optimize" parameters first, then SDelete with -z option for "zero free space".


JkDefrag-3.36\JkDefragCmd.exe -a 3
SDelete\sdelete.exe -z c:
C:\scripts>JkDefrag-3.36\JkDefragCmd.exe -a 3
JkDefrag v3.36
Date: 2014/05/17
Windows version: v5.1 build 2600 Service Pack 3
Commandline argument '-a' accepted, optimizemode = 3
Phase 1: Analyze
This is an NTFS disk.
Phase 2: Defragment
Phase 3: Fixup
Zone 1: Fast Optimize
Zone 2: Fast Optimize
Zone 3: Fast Optimize
Phase 3: Fixup

C:\scripts>SDelete\sdelete.exe -z c:

SDelete - Secure Delete v1.61
Copyright (C) 1999-2012 Mark Russinovich
Sysinternals -

Free space cleaned on C:\
1 drives zapped


And finally we create a shortcut to the script(s) on the desktop.

Download the scripts used in this article:

In this article VirtualBox Version 4.3.10 was used to run Windows XP SP3 virtually, and test the scripts mentioned above.

Site maintained by
Page generated in: 0.0435 seconds.
Memory usage: 535864 bytes (peak: 551336 bytes)