By default, VirtualBox creates dynamic disks that grow over time as you add data. However, if you delete data from the virtual machine later, you’ll notice that the disk doesn’t automatically shrink. But you can manually shrink a dynamic disk using a hidden command.
RELATED: Beginner Geek: How to Create and Use Virtual Machines
Remember that this will only shrink the disk if it’s grown in size and you’ve since removed data. So, if you just created a dynamic disk, this won’t shrink it. But, if you’ve created a dynamic disk, downloaded 10 GB of data into it, and then deleted that 10 GB of data afterwards, you should be able to shrink the disk by about 10 GB.
Step One: Ensure You’re Using a Dynamic Disk
This process only works for dynamic disks, which can grow and shrink in size. Dynamic disks may be up to a certain maximum size—50 GB, for example—but they only actually grow to that maximum size when they contain that much data. Fixed size disks will always be their maximum size.
RELATED: How to Convert Between Fixed and Dynamic Disks in VirtualBox
If you have a fixed size disk you want to shrink, you can first convert it to a dynamic disk and then follow the below instructions. For example, if you have a fixed disk of 50 GB in size with only 20 GB of data on it and you convert it to a dynamic disk, you should be able to shrink it to take up only 20 GB of space.
To check whether a disk is dynamic or fixed size in VirtualBox, right-click the virtual machine that uses the disk and select “Settings”. Click the “Storage” tab and select the disk. You’ll see what type of disk it is displayed next to “Details”. For example, in the screenshot below, “Dynamically allocated storage” indicates that this is a dynamic disk.
Step Two: Write Zeros to the Disk in The Virtual Machine
Be sure to delete any data you no longer want on the disk inside the virtual machine to free up space before continuing. Delete files you don’t need, uninstall programs you no longer use, and empty your Recycle Bin. Then, you’ll need to overwrite that empty space with zeroes.
For Windows Guest Operating Systems
If you have Windows installed inside the virtual machine, you should now boot the virtual machine up and defragment its disks. Inside the virtual machine, search the Start menu for “Defragment” and launch the “Disk Defragmenter” or “Defragment and Optimize Drives” tool. Select the disk you want to compact and click “Defragment disk”.
After the defragmentation process is finished, you will need to write zeros to the empty space inside the virtual machine. When you delete files, the deleted data is still stored on the disk so VirtualBox can’t automatically shrink the drive. But, when you write zeros over the deleted files, VirtualBox will see a large amount of zeroes—empty space, in other words—and be able to compact the disk.
To do this, download the SDelete utility from Microsoft. Extract the sdelete.exe file to a folder on your computer.
Open a Command Prompt window. To do so, open the Start menu, search for “Command Prompt”, and launch the shortcut.
Change to the directory containing the sdelete.exe file by typing cd , pressing Space, entering the path to the directory, and pressing Enter. Be sure to enclose the path in quotation marks if it contains a space character. It should look like this:
For example, if you extracted the sdelete.exe file to your user account’s download folder and your Windows username is Bob, you’d run the following command:
To quickly fill in the directory path, simply type cd into the Command Prompt window, press Space, and then drag and drop the folder icon from the file manager’s address bar.
Run the following command:
This will write zeros to all the free disk space on drive C:. If you want to shrink a secondary drive located at a different drive letter in the virtual machine, type its drive letter instead of c:. This is what the tool was designed for. As the SDelete page on Microsoft’s website notes, the -z option is “good for virtual disk optimization”.
You’ll be asked to agree to the tool’s license agreement before continuing. Just click “Agree”.
Wait for the process to complete. When it’s done, shut down your virtual machine using the “Shut Down” option in its Start menu. You’re now ready to compact it.
For Linux Guest Operating Systems
If you have Linux installed in the virtual machine—a Linux guest operating system instead of a Windows guest operating system, in other words—you can skip the defragmentation process and use built-in commands to zero the free space on the drive. We’ll use Ubuntu as the example here, but the process will be similar on other Linux distributions.
To do this, you’ll first need to install the zerofree utility inside the virtual machine. It should be available in your Linux distribution’s software repositories. For example, you can install it on Ubuntu by running the following command at the terminal inside your virtual machine:
You can’t actually use zerofree on your / partition while you’re booted into the standard Linux environment. Instead, you’ll want to boot into a special recovery mode where your normal root partition isn’t mounted. On Ubuntu, restart your virtual machine, and repeatedly press the “Esc” key while it’s booting to access the Grub menu. When the Grub menu appears, select “*Advanced options for Ubuntu” and press Enter.
Select the “(recovery mode)” option associated with the most recent Linux kernel—that is, the option with the highest version number near the top of the list—and press Enter.
Select “root” in the recovery menu to boot to a root shell prompt.
Press “Enter” afterwards when “Press Enter for maintenance” appears on your screen. You’ll be given a terminal prompt.
At the command line, determine which virtual disk you want to zero by running the following command:
In the output below, we can see that /dev/sda1 is our only actual disk device here. We know that because it’s the only disk with /dev/ in the leftmost column.
Assuming your virtual machine was created with the default settings, it will only have /dev/sda1 , which is the first partition on the first hard drive. If you’ve set things up differently with multiple disks or multiple partitions, you may need to zero another partition or zero multiple partitions.
You’re now ready to zero the disk. Run the following command, replacing /dev/sda1 with the device name of the partition you want to write zeros to. Most people will just have a /dev/sda1 device to zero.
When the zero process is finished, run the following command to shut down the virtual machine:
When you see the “System halted” message on your screen, the system has halted and you can now shut down your virtual machine. Close the virtual machine’s window and select “Power off the virtual machine”.
Step Three: Find the VBoxManage Command
The rest of the process will be performed outside the virtual machine, on your host operating system. For example, if you have Windows 10 running on your PC and Windows 7 running in a virtual machine, you’d perform the rest of the process on Windows 10.
This option isn’t exposed in VirtualBox’s graphical interface. Instead, you have to use the VBoxManage.exe command.
Locate this command to continue. On Windows, you’ll find it in the VirtualBox program directory, which is C:\Program Files\Oracle\VirtualBox by default. If you installed VirtualBox to another directory, look there instead.
Open a Command Prompt window. To do this, open the Start menu, type cmd and press Enter.
Type cd into the Command Prompt, followed by the path of the folder where the VBoxManage command is. You’ll need to enclose it in quotes.
You can quickly do this by typing cd into the Command Prompt window, and then dragging and dropping the folder icon from the file manager’s address bar into the Command Prompt.
If you’re using the default path, it should look like the following:
NOTE: These instructions assume you’re using VirtualBox on Windows. If you’re using VirtualBox on macOS or Linux, you can just open a Terminal window and run the vboxmanage command normally, as you would any other command.
Step Four: Locate the Path to the Disk You Want To Compact
Run the following command in the command prompt window to view a list of all the virtual hard disks on your computer:
Look through the list and identify the file path to the virtual disk you want to compact. Let’s say we want to modify the virtual disk associated with the virtual machine named “Windows 7”. As we can see in the output below, the path to that virtual disk on our system is C:\Users\chris\VirtualBox VMs\Windows 7\Windows 7.vdi .
Step Five: Compact the Disk
To compact the disk, you’ll need to use VBoxManage.exe with the correct command. Just run the following command, replacing the path to the disk VDI file with the path to the disk on your system which you found using the above command. Be sure to enclose the path of the file in quotes if it contains a space anywhere in its path.
It should look like this:
For example, since the path to the file is C:\Users\chris\VirtualBox VMs\Windows 7\Windows 7.vdi in our example, we’d run the following command:
VirtualBox will compact the disk and you can immediately boot up the virtual machine afterwards, if you like. How much space you end up saving depends on how much empty space there was to recover.
Also Consider Deleting Snapshots
RELATED: How to Save Time by Using Snapshots in VirtualBox
VirtualBox allows you to create snapshots for each virtual machine. These contain a full image of the virtual machine when you created the snapshot, allowing you to restore it to a previous state. These can take a lot of space.
To free up more space, delete snapshots you don’t use. To view the snapshots you have saved for a virtual machine, select it in the main VirtualBox window and click the “Snapshots” button to the right of Details on the toolbar. If you no longer need a snapshot, right-click it in the list and select “Delete Snapshot” to free up space.