Shrink Snapshot VDI [VirtualBox]

Abstract: Resolve large VirtualBox VDI file sizes by merging snapshots, zero-filling guest OS free space, and running vboxmanage modifymedium --compact.

Created: 2026-08-27 by
Shrinking a VirtualBox VDI snapshot directly is impossible because snapshots store differences from a base disk, not the actual file size. To reduce the VDI file size, you must delete the snapshot chain to consolidate the data into the base virtual disk, then run a zero-filling operation inside the guest OS to mark unused space as empty, and finally execute the vboxmanage modifymedium command with the --compact flag on the host system.

Understanding Why Direct Shrinking Fails

VirtualBox uses a differencing disk mechanism for snapshots. When you create a snapshot, the current state of the virtual hard drive (VDI) becomes the "base" or parent disk. Any subsequent changes are written to a new child VDI file linked to that snapshot. The total size of the snapshot chain includes the base disk plus all delta files. Because the snapshot metadata points to specific blocks in these files, VirtualBox cannot simply trim the file size without risking data corruption or breaking the chain. This is why tools like sdelete inside the guest often appear ineffective if you do not also manage the snapshot structure correctly.

Step 1: Merge Snapshots via Snapshot Manager

Before you can shrink the underlying disk, you must remove the snapshot layering. This process merges the changes from the snapshot back into the base disk, effectively creating a single, larger VDI file that contains all your data up to that point. You can perform this through the VirtualBox Manager interface or the command line. Open the VirtualBox Manager and select your VM. Click on the Snapshots icon (usually a camera symbol) to open the Snapshot Manager. If you have multiple snapshots, you need to merge them one by one or revert to the base and delete the top-most snapshot. Select the snapshot you want to remove and click Delete. Ensure you choose the option to Discard the snapshot's saved state, which merges the changes. For command-line users, you can list snapshots using:
vboxmanage snapshotlist <VM_NAME>
To delete a specific snapshot named Snapshot1, use:
vboxmanage snapshot <VM_NAME> delete "Snapshot1"
Repeat this until only the base disk remains. Note that after merging, the VDI file may actually grow larger than before because it now contains all the historical data previously stored in delta files.

Step 2: Zero-Fill Unused Space Inside the Guest

Even after merging snapshots, the VDI file will still show as large because the guest operating system does not know that those blocks are no longer referenced by the snapshot chain. The file system marks them as free, but the VDI driver still allocates space for them. To allow the host to reclaim this space, you must write zeros to the unused sectors. If you are using Windows inside the VM, use the built-in sdelete tool. Open Command Prompt as Administrator and run:
sdelete -z C:
This command writes zeros to all free space on the C: drive. For Linux guests, you can use the dd command to fill free space with zeros:
sudo dd if=/dev/zero of=/tmp/zero_file bs=1M status=progress
rm /tmp/zero_file
Ensure the file is deleted immediately after creation so it does not consume disk space permanently. Some modern file systems support TRIM/Discard commands, which can automate this process if enabled in the VM settings, but manual zero-filling is more reliable for ensuring full compaction.

Step 3: Compact the VDI File on the Host

Once the guest has been zero-filled, the host system can safely discard the empty blocks. Use the vboxmanage modifymedium command with the --compact option. This tells VirtualBox to scan the VDI file and remove any blocks that contain only zeros. First, ensure the VM is completely powered off, not suspended or saved. Then, locate the path to your VDI file. You can find this in the VM settings under Storage. Run the following command in your host terminal:
vboxmanage modifymedium disk "/path/to/your/disk.vdi" --compact
Replace the path with the actual location of your VDI file. The process may take some time depending on the size of the disk. Once complete, check the file size; it should be significantly reduced, reflecting only the actual data used by the guest OS.

Troubleshooting Common Issues

If the file size does not decrease after compacting, verify that the zero-fill step was successful. Sometimes, fragmented files or active system processes prevent the guest from writing zeros to all free space. Rebooting the guest and running the zero-fill command again can help. Additionally, ensure that the VDI is not attached to another VM or locked by a background process. For more details on managing VDI files and understanding the differences between various disk formats, you might find Differences Between Ubuntu VDI and VHDX Files in VirtualBox: Safely Deleting CanonicalGroupLimited Folder useful. Furthermore, if you encounter issues with snapshot management specifically, refer to Shrinking a Snapshot of a VDI in VirtualBox for advanced techniques.

Frequently Asked Questions

Can I shrink a VDI while the VM is running?

No, you must power off the virtual machine completely before running the --compact command. Running it on a powered-on or suspended VM can lead to data corruption and invalid disk states.

Why did my VDI get bigger after deleting a snapshot?

Deleting a snapshot merges its changes into the base disk. Since the base disk now contains all the data from the snapshot period, its size increases to accommodate the newly consolidated information. This is a necessary intermediate step before compaction.

Does TRIM work automatically in VirtualBox?

TRIM (or Discard) can work automatically if enabled in the VM settings and supported by the guest OS file system. However, it is not always enabled by default, and manual zero-filling provides a more guaranteed result for significant size reductions.

What if the compact command fails or hangs?

Check that no other process is accessing the VDI file. Ensure the VM is not in a paused state. If it hangs, try restarting the host computer to release any file locks, then attempt the command again.

Is it safe to delete snapshots regularly?

Yes, but only if you have backups. Deleting snapshots consolidates data, reducing fragmentation and file size overhead. However, once deleted, the previous state cannot be restored unless you have an external backup of the base disk.

Latest News

How to Display Output of xterm Command in Terminal Screen using Cron

Resolve the inability to display xterm output in a terminal screen from cron by redirecting logs or using wall instead of insecure X11 workarounds.

Read More
Expose Local Server on VPS Preserving Real Client IPs (OS Level)

Resolve VPS logging showing only gateway IPs by configuring reverse proxies to forward real client addresses via X-Forwarded-For headers.

Read More
How to Clear Recent Files History in Linux without Deleting Actual Files?

Clear recent files history in Linux by deleting ~/.local/share/recently-used.xbel without removing the actual files.

Read More
Best HDD Copy Strategy: Manually Syncing and Rewriting Scratch

Resolve data drift between two 4TB hard drives by using rsync with the --delete flag to manually sync and rewrite scratch data for an exact mirror.

Read More
Understanding Bash Prompt Abbreviations: An Overview

Resolve repetitive typing errors by creating and persisting Bash aliases in the .bashrc file for improved command-line efficiency.

Read More
Free Tools and Methods to Log Real-Time Performance Details in Task Manager

Log real-time CPU and memory metrics to CSV files using Windows Performance Monitor Data Collector Sets to troubleshoot system slowness.

Read More
Contact Qatar Airways Customer Service: Phone Number, Help Center, and More

Resolve urgent Qatar Airways reservation issues like flight changes and baggage claims using the US phone number +1801-855-5905 or online self-service tools.

Read More
Windows 11: Alt Codes Displaying Wrong Characters - Solutions

Resolve Windows 11 Alt codes showing wrong characters by verifying the US keyboard layout and enabling Num Lock.

Read More
Run Programs with Elevated Privileges: A Comprehensive Guide

Eliminates the need to type sudo for frequent administrative tasks by configuring .bashrc aliases or functions.

Read More
Dynamically Changing Dates in Excel Formulas Using Cells

Eliminate manual date updates in Excel by using cell references to create dynamic formulas that automatically adjust calculations when input dates change.

Read More
Granting Edit Access to a Specific Excel Sheet in an Office 365 User Email ID-Based Login

Excel Online lacks native sheet-level permissions, so users must split workbooks or use worksheet protection to isolate edit access for specific tabs.

Read More
Creating Network Load on Local Wi-Fi from One PC

Generate sustained Wi-Fi bandwidth load from a single PC using a VM SFTP server to test router stability and throughput.

Read More
Changing the Default Editor for Batch Files in Windows 11's File Explorer

Resolve the issue of batch files opening in Notepad by changing the default editor to a preferred application like VS Code.

Read More
Gain WiFi Connection Minutes Before Losing it by Changing MAC Address

Bypass MAC address filtering to restore WiFi connectivity temporarily when a device is blocked by network administrators.

Read More
Photos Folders Turned Black: Troubleshooting Picture Applications on Windows

Resolve black screens in Windows Photos by resetting the app, updating graphics drivers, and repairing system files.

Read More
Troubleshooting High CPU Temperature Values: A Step-by-Step Guide

Resolve high CPU temperatures and thermal throttling by monitoring with Speedfan, cleaning dust from cooling components, and replacing degraded thermal paste.

Read More
Make cURL establish TLS connection with CA unknown

Resolve cURL TLS connection failures with unknown or self-signed certificates by using the --insecure flag to bypass CA validation.

Read More
Troubleshooting Incorrect Network Metrics Values in Grafana, Prometheus, InfluxDB, and Telegraf

Fix incorrect network metrics in Grafana, Prometheus, InfluxDB, and Telegraf by verifying interface selection, resolving unit mismatches, and handling counter resets.

Read More
Start Another Terminal Window (cmd.exe) Windows Repair/Troubleshoot

Provides step-by-step instructions to access the Windows Command Prompt via bootable media for advanced system repair and troubleshooting.

Read More
Thinkpad P1 Gen 6: Sudden Thunderbolt 3 Dock USB-C Charging Stopped Working

Resolve sudden Thunderbolt 3 dock USB-C charging and internet failure on ThinkPad P1 Gen 6 by updating firmware, resetting the dock, and adjusting power settings.

Read More
ASUS Windows 11 Laptop: Internet Connection Issue with Hotspot

Resolve ASUS Windows 11 laptop spinning wheel issue when connecting to mobile hotspot by resetting network stack, disabling IPv6, and adjusting power settings.

Read More
Developing iOS Project with Firebase: Logging in and Sending Push Notifications

Integrates Firebase to enable secure user authentication and push notifications in iOS applications.

Read More
Troubleshooting Bash Script with Suspected Issues of Spaces, Dashes, Filenames and Quoting

Resolve Bash script failures caused by word splitting, globbing, and special characters in filenames by applying proper quoting techniques.

Read More
Populating OneDrive Folder for Local Backup without Affecting Remote Files

Prevent OneDrive from deleting remote files during Windows reinstallation by unlinking the account or using symbolic links to isolate local data.

Read More
Force First Frame as Keyframe Without Re-encoding: A Quick Solution

Resolve inaccurate seeking and playback glitches by re-encoding video to ensure the first frame is a valid keyframe.

Read More
Find Information in Database Using WSL (Ubuntu) on Windows-10

Resolve database connection issues from WSL Ubuntu on Windows 10 by configuring client tools and network access to query remote or local servers.

Read More
Configuring a New Router with Different Networks: One Family, One Guest

Resolve IP conflicts and isolate guest traffic by configuring a second router with a distinct subnet via its WAN port.

Read More
Quickly Open Multiple Stock Websites with a Single Command

Solve the problem of manually opening multiple stock research websites by creating a single command to launch them simultaneously on Windows, macOS, or Linux.

Read More
Improved Adobe Acrobat Search Experience: Access Advanced Search with Ease

Access advanced search filters like case sensitivity and regex in Adobe Acrobat by clicking More Options in the Find bar.

Read More
Port Forwarding VM via OpenVPN: A Comprehensive Guide

Configure OpenVPN server and iptables to forward specific ports from VPN clients to a virtual machine's internal IP address.

Read More
Understanding Partitions in the 1980s: A Look Back at MS-DOS and Master Boot Record

Explains how the Master Boot Record and partition table enabled MS-DOS to manage multiple disk volumes on a single physical drive.

Read More
Windows 10 Enterprise: Keyboard Not Detected in Managed Organization with Admin Privileges

Resolve Windows 10 Enterprise keyboard detection failures caused by Group Policy restrictions, driver corruption, or USB enumeration errors.

Read More
Can't Find x86_64 Versions of Applications on MacOS arm64 (M1)

Resolve missing x86_64 app issues on M1 Macs by installing Rosetta 2 and reinstalling apps as universal binaries.

Read More
How to Hide Installation Source for an Android App

Manage Android app installation source visibility by using USB transfers or file managers to reduce browser traces and isolate data via work profiles.

Read More
Connecting Momentus 4200.2 ST9808210A Hard Drive via USB Cable

Resolve inability to access Seagate Momentus 4200.2 ST9808210A hard drive by connecting it via USB using a compatible SATA-to-USB adapter and configuring the disk in Windows.

Read More