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 TheVirtual 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 WindowsGuest 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 SDeleteutilityfrom 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 shouldlook 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
cdinto 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:
sdelete.exe c: -z
This will write zeros to all the free disk space on drive C:. If you want to shrink a secondary drive located ata 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 licenseagreement 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:
sudo apt install zerofree
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 isthe 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.
zerofree -v /dev/sda1
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 beperformed 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.
Thisoption isn’t exposed in VirtualBox’sgraphical interface. Instead, you have to use the
Locate this command to continue. On Windows, you’ll find it in the VirtualBox program directory, which is
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
cmdand press Enter.
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:
cd "C:\Program Files\Oracle\VirtualBox"
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
vboxmanagecommand normally, as you would any other command.
Step Four: Locate the Path to the Disk You Want ToCompact
Run the following command in the command prompt window to view a list of all the virtual hard disks on your computer:
VBoxManage.exe list hdds
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:
VBoxManage.exe modifymedium disk "C:\path\to\disk.vdi" --compact
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:
VBoxManage.exe modifymedium disk "C:\Users\chris\VirtualBox VMs\Windows 7\Windows 7.vdi" --compact
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, selectit in the main VirtualBox windowand 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.
- › Your Favorite Reddit App Is About to Shut Down
- › How to Stop Your PS5 From Overheating
- › Vision Pro Fails to Fit Into Apple’s Digital Wellbeing Mission
- › Google Chrome Now Has More Password Manager Features
- › Mozilla Thunderbird’s Next Big Update Is Now in Beta
- › Bing Chat on the Web Now Supports Voice Input
- Open the VMware Tools Control Panel / Toolbox: In Windows: Double-click the VMware Tools icon in the system tray, or click Start > Control Panel > VMware Tools. In Linux: ...
- Click the Shrink tab.
- Select the drive you want to shrink.
- Click Prepare to Shrink, then follow the onscreen instructions.
- Deploy a new disk with a new size. To reduce the size of a system disk on an existing server, you will need to deploy a new disk with the desired size and then swap it for the old disk. ...
- Preparing the server with the boot ISO. ...
- Clone the disk. ...
- Set the new disk as the root.
- Right-click the virtual machine, and click Migrate.
- Click Change datastore.
- Click Next, and select a datastore that is not the same as the current datastore.
- From the dropdown, select the Thin Provision virtual disk format.
The files keep growing because only used sectors are stored in the file. And Windows happily keeps data in the "used" sectors even if they are freed in the guest OS. And because there is data in there, the VM cannot clean it up.How do I reduce disk capacity? ›
One of the easiest ways to clean up files you no longer need is by using Disk Cleanup. Open Disk Cleanup by clicking the Start button . In the search box, type Disk Cleanup, and then, in the list of results, select Disk Cleanup. If prompted, select the drive that you want to clean up, and then select OK.How to reduce the hard disk size for a virtual machine in vmware? ›
- Select Window > Virtual Machine Library.
- Select a virtual machine in the Virtual Machine Library window and click Settings.
- In the Settings window, click the hard disk you want to resize.
- Use the Disk size slider to set the new size.
- Open the Azure portal.
- Open the page for the virtual machine.
- In the left menu, select Size.
- Pick a new size from the list of available sizes and then select Resize.
Re: Maximum size of virtual disk? You can create hard disks with up to 2TB with the GUI.How do I increase the size of my C drive in virtual machine? ›
- Right-click the VM and select Connect. ...
- Enter DISKMGMT. ...
- Right-click the C: drive and select Extend Volume. ...
- Select the available amount of space by which you want to extend the volume. ...
- Check that you have selected the correct settings.
Storage: VirtualBox requires only about 30 MB of hard disk space. You will need enough storage to house your VMs, and VMs can easily start at 10 GB each.
- Step One: Locate the VBoxManage Command and Open a Command Prompt. ...
- Step Two: Locate the Path to the Disk You Want To Convert. ...
- Step Three: Convert the Virtual Disk. ...
- Step Four: Remove the Old Virtual Disk. ...
- Step Five: Rename the New Disk.