But over time the virtual machine (VM), also known as the guest operating system, may grow due to files you have saved and other activities you’ve done inside the OS. Funnily enough, deleting files directly from inside the VM doesn’t free up the storage space for the host system to use. If you have a need to free up more storage space, these are the steps to shrink a VirtualBox VM and reclaim lost space.
Dynamic Memory vs. Fixed Size Memory
If you selected the “Fixed Size” option when you first created the VM, this guide won’t work for you. The reason is because you’ve already agreed to a fixed size for VirtualBox to use for the guest OS. It can’t grow nor shrink beyond a set limit. If you selected “Dynamic Size” upon creating the VM, then you are set for the next steps. Dynamic Size allows for the OS to grow its storage allocation dynamically.
1. Delete and Clear Space in Guest OS
You can do this for both Windows and Linux OS.
For Windows as Guest Operating System
- Delete unwanted files inside the Windows guest OS. Go to the Recycle Bin inside your guest OS and delete all the unnecessary files. Uninstall all unwanted applications and remove all unnecessary backups (like Windows.old, old Windows restore points, etc.). The goal is to free up as much space as you can from inside the guest OS.
- Open Disk Defragmenter. If your guest OS is Windows 10, just search for “defragment” in the Start menu and open “Defragment and Optimise Drives” option.
Choose the disk you want to defragment and click “Optimise”.
Download the SDelete file. This is a command line utility to securely delete files from the system. Extract the SDelete utility to the Desktop. You should find 3 files in the folder: Eula, sdelete and sdelete64.
Next, open the command prompt and cd to the SDelete folder. Run the following command:
The -z flag instruct it to zero free space, which is required for virtual disk optimization. Zeroing free space means fill up all the free space with zeroes, so they can be removed and reclaimed later on. 5. As soon as it finishes this process, shut down your Windows guest and VirtualBox. Now you should be back to your native Windows 10 OS.
For Linux as Guest Operating System
If Linux is what you have installed on your VirtualBox, then follow the next step to free memory internally. Note: we are using Ubuntu for this tutorial.
Start the Linux guest from Virtualbox. On bootup, press the “Esc” button repeatedly to initiate the advanced boot interface.
Multiple boot options will be shown to you. Select “Advanced options for Ubuntu.”
Select “Recovery Mode.”
Choose the root folder when prompted.
Next, choose “boot to recovery” and press Enter for maintenance.
Inside the command interface, run the following commands: A screen will appear with a list of all the drives attached for a Linux guest. Find the one you want to clear. (In this case it is “/dev/sda1.”)
Run: This code will find the unallocated blocks with non-zero value content and fill them with zeroes so they can be reclaimed later.
As soon as the program is done executing, run: This command shuts down the Linux OS. Now power down VirtualBox and close the application.
2. Reclaim Freed Space in Guest OS
After you have freed up storage space in the guest OS, it is time to reclaim the space and add it back to your host OS.
Open command prompt inside Windows 10. Inside the command prompt run this command:
A new shell program named DISKPART should come up. Select Yes when it prompts for admin access.
Inside Diskpart you will need to specify the path to your VDisk file. This file is the virtual hard drive for your guest OS. Once you have it, copy the path and run: Click Enter to run.
When it completes its process, run:
Run:
Wait for it to complete this process, then run: Now all the space you have freed inside your guest OS should be reclaimed by your native Windows 10 OS.
Wrapping Up
If you are not careful about your VirtualBox usage, the guest OS ‘s size can grow exponentially without your knowledge. When that happens, and you are running out of storage space in your host system, this tutorial will help you to shrink VirtualBox VM and reclaim the lost space so you can put it to other use.