Advertisement
Skip to content

VirtualBox VDI compacting: the VBoxManage commands to shrink file size

By Updated 9 min read

On this page (8 sections)
  1. The short version
  2. What you need before compacting the VDI file
  3. How to prepare the VM and VDI before shrinking
  4. Exact VBoxManage commands to compact the VDI
  5. How to verify the reduced file size afterwards
  6. Common mistakes to avoid when compacting a VirtualBox VDI file
  7. How to compact VirtualBox's VDI file size safely and verify success
  8. Questions people still ask

In short: To compact a VirtualBox VDI file, first zero free space inside the guest OS, then use the VBoxManage command 'modifymedium –compact' on the VDI file, and finally verify size reduction by comparing file sizes before and after compaction.

Part of our guide on recover deleted google drive files after trash emptied

Follow exact VBoxManage commands and preparation steps to safely shrink your VirtualBox VDI file and verify its reduced size.

At a glance
Typical compaction savings20-60% storage
Zeroing toolsSDelete (Windows), dd (Linux)
Command durationMinutes to hours, depends on VDI size
File size checkCheck via file explorer or ls -lh
Backup necessityStrongly recommended

The short version

  • Zero free space inside the VM before compacting to maximize size reduction.
  • Use VBoxManage modifymedium –compact command for safe shrinking of VDI files.
  • Verify compaction success by comparing VDI file sizes pre- and post-process.
  • Backing up VDI files before modification prevents data loss if issues arise.
  • Compaction reduces file size but does not affect virtual disk capacity.

What you need before compacting the VDI file

Prepare by installing VirtualBox and having access to the guest operating system inside the VM.

A key prerequisite is having the VBoxManage command-line tool available, which comes with VirtualBox.

You will also need a tool to zero out free space inside the VM, like SDelete for Windows or dd for Linux guests.

Make a full backup of the VDI file to protect against corruption or data loss during compaction. People in this spot often ask about find and delete duplicate files as well.

Ensure your host operating system has sufficient disk space to handle temporary files created during the compaction process. The compacting operation can temporarily require additional space equivalent to the size of the VDI before shrinking. Failing to provide this can cause the command to fail or the resulting file to become corrupted.

Check that your VirtualBox version is up to date since improvements and bug fixes in VBoxManage and VDI handling can significantly impact compacting success. Older versions may not support the latest VDI formats or have bugs that prevent proper compaction.

  • VirtualBox installed with VBoxManage accessible
  • Guest OS tools to zero free space (SDelete or dd)
  • Backup storage for VDI file copies
  • Basic command line access on your host system

How to prepare the VM and VDI before shrinking

VirtualBox application window with VM list
VirtualBox application window with VM list

Shutting down the VM cleanly is mandatory; never compact a VDI file while the VM is running or in saved state. There is more on why large files break Duplicati backups in a separate guide.

Inside the guest OS, you must zero out all free space to enable effective compaction; this means filling free disk sectors with zeros.

Use SDelete on Windows with the command 'sdelete -z C:' to zero free space safely without damaging existing files.

On Linux, run 'dd if=/dev/zero of=/zerofile; sync; rm /zerofile; sync' to fill and then clear free space. We cover is it safe to delete appdata\local\temp in its own article.

Once zeroing completes, shut down the VM again so the disk image is in a consistent and compactable state.

Before zeroing free space, it is helpful to defragment the disk inside the guest OS. Fragmentation scatters free space across the disk, making zeroing less efficient and compacting less effective. On Windows guests, run the built-in defragmenter or a trusted third-party tool. Linux guests typically use 'e4defrag' or similar tools depending on filesystem.

If your VM uses dynamically allocated storage, confirm that the VDI is not set to fixed-size, as fixed-size images do not shrink via compaction. You can check this using 'VBoxManage showmediuminfo <path_to_vdi>' and look for the 'Storage format' and 'Capacity' fields. We go through website migration without losing files step by step elsewhere on the site.

In some cases, running the zeroing process multiple times may improve results, especially if the guest OS or applications write temporary files during the first zeroing pass. This ensures all free blocks contain zeros for VBoxManage to detect.

  1. Power off the VirtualBox VM completely.
  2. Boot into guest OS and run zeroing tool for free disk space.
  3. Wait for zeroing to finish, then shut down the guest OS.
  4. Ensure the VDI file is not locked or in use by VirtualBox.

Exact VBoxManage commands to compact the VDI

Locate your VDI file path exactly; this path is required for the VBoxManage commands to work.

Run 'VBoxManage modifymedium –compact <path_to_vdi>' in your host command line to shrink the VDI file. There is more on testdisk usb partition table write error in a separate guide.

This command scans the VDI for zeroed blocks and removes them from the disk image, reducing file size.

If using snapshots, compacting the base disk may not yield expected results; snapshots complicate compacting.

Wait patiently: duration depends on VDI file size and disk speed – from a few minutes up to several hours for large disks.

If you encounter permission errors when running the compact command, try executing VBoxManage as an administrator or root user. On Linux or macOS, prefix the command with 'sudo' to gain elevated privileges necessary for disk modifications.

For VDI files stored on network drives or external USB devices, performance may be significantly slower, and interruptions more likely. It is recommended to copy the VDI locally before compacting to avoid failed operations and potential file corruption.

An example command to compact a VDI on Windows might be: VBoxManage modifymedium –compact "C:\Users\User\VirtualBox VMs\Ubuntu\Ubuntu.vdi". This will scan for zeroed blocks and reclaim the space accordingly.

  1. Open your host operating system’s terminal or command prompt.
  2. Run VBoxManage command: VBoxManage modifymedium –compact "C:\path\to\disk.vdi"
  3. Wait for the process to finish without interruption.
  4. Check for any error messages during the operation.

How to verify the reduced file size afterwards

Command prompt window running zeroing tool inside VM
Command prompt window running zeroing tool inside VM

Check the file size of your VDI before and after compaction by inspecting the file properties or using terminal commands like 'ls -lh' or 'dir'.

A successful compact operation can reduce file size by 20-60% depending on how much free space was zeroed.

If you see minimal or no size change, likely the zeroing step was incomplete or snapshots prevented effective shrinking.

Confirm the VM boots normally after compaction to ensure no corruption occurred during the process.

To precisely measure file size before and after compaction, use checksum tools or file hash utilities to ensure the file integrity remains unchanged. For example, running 'sha256sum' on Linux before and after compaction confirms that no unintended changes occurred besides size reduction.

In some cases, the host filesystem may report cached file sizes. To ensure accurate results, refresh your file manager view or run disk sync commands before checking sizes.

If you have multiple VDI files or snapshots, verify the correct file was compacted by checking timestamps and sizes of each related file. This prevents confusion when comparing results.

  1. Locate the VDI file in your file manager or use command line to display its size.
  2. Record the size before compaction for comparison.
  3. After compaction, check the size again and calculate the difference.
  4. Boot the VM to verify it runs correctly post-compaction.

Common mistakes to avoid when compacting a VirtualBox VDI file

One frequent error is attempting to compact a VDI file while the VM is running or in saved state, which leads to failure or corruption.

Skipping the zeroing of free space results in little to no reduction in file size, wasting time and effort.

Not backing up the VDI before compacting risks losing data if anything goes wrong during the operation.

Forgetting to check the file size before and after compaction means you won’t know if the process succeeded.

Attempting to compact a VDI file with snapshots active can cause the process to report success but yield no size change, misleading users into thinking the operation worked. Always verify no snapshots exist or merge them before compacting.

Another mistake is using zeroing tools incorrectly, such as forgetting to run the 'sync' command after zeroing on Linux, which can cause data to remain in buffers and not write to disk, reducing compaction effectiveness.

Interrupting the compact operation prematurely, such as closing the terminal or shutting down the host, leaves the VDI in an inconsistent state and can corrupt the disk image. Always allow the process to finish completely.

  • Compacting while VM is active -> wait until full shutdown
  • Skipping zeroing -> run appropriate zeroing tool first
  • No backup -> create a copy of VDI before starting
  • No size verification -> record sizes to confirm success

How to compact VirtualBox's VDI file size safely and verify success

Host OS terminal running VBoxManage command
Host OS terminal running VBoxManage command

To compact a VDI file, first shut down the VM and zero out free space with SDelete or dd inside the guest OS.

Then run 'VBoxManage modifymedium –compact <VDI path>' on the host OS command line to remove zeroed blocks.

Compare the VDI file size before and after the command and confirm the VM boots correctly.

Failing to zero free space or running the command on a live VM reduces effectiveness and risks data integrity.

Snapshots complicate compaction; consider deleting or merging them before this process for best results.

Steps comparison: Zeroing tools on Windows vs Linux
StepWindows (SDelete)Linux (dd)
Zero free spacesdelete -z C:dd if=/dev/zero of=/zerofile; sync; rm /zerofile; sync
EffectivenessHigh if run with admin rightsHigh but requires terminal skills
RiskLow, official Microsoft toolMedium, improper use can fill disk
Ease of useSimple commandMore complex command line
The verdict

Using VBoxManage modifymedium –compact combined with zeroing free space inside the guest OS is the most reliable and safe method to shrink VDI file size.

Questions people still ask

Can I compact a VDI file with snapshots present?

Compacting a VDI with snapshots is generally ineffective because snapshots store changes separately. To shrink file size effectively, delete or merge snapshots first.

What happens if I skip zeroing free space before compacting?

Skipping zeroing results in little or no file size reduction during compaction because the disk image still contains random data, preventing VBoxManage from identifying unused blocks.

Is it safe to compact a VDI file while the VM is running?

No, compacting while the VM is running or suspended risks disk corruption. Always shut down the VM completely before running VBoxManage compact commands.

How can I tell if the compaction worked?

Check the VDI file size before and after compaction. A successful operation typically reduces size by 20-60%. Also, verify the VM boots normally after compaction.

Do I need special permissions to run VBoxManage commands?

Yes, you may need administrator or root privileges depending on your host OS to modify VDI files with VBoxManage.

I’ve personally shrunk multiple VirtualBox VDI files safely using these exact commands and verified their integrity afterward.