How to Shrink a VirtualBox Virtual Machine and Free Up Disk Space (2023)

How to Shrink a VirtualBox Virtual Machine and Free Up Disk Space (1)

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.

How to Shrink a VirtualBox Virtual Machine and Free Up Disk Space (2)

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”.

How to Shrink a VirtualBox Virtual Machine and Free Up Disk Space (3)

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.

How to Shrink a VirtualBox Virtual Machine and Free Up Disk Space (4)

Open a Command Prompt window. To do so, open the Start menu, search for “Command Prompt”, and launch the shortcut.

How to Shrink a VirtualBox Virtual Machine and Free Up Disk Space (5)

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:

cd "C:\path\to\folder"

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:

cd "C:\Users\bob\Downloads"

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.

How to Shrink a VirtualBox Virtual Machine and Free Up Disk Space (6)

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”.

How to Shrink a VirtualBox Virtual Machine and Free Up Disk Space (7)

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.

How to Shrink a VirtualBox Virtual Machine and Free Up Disk Space (8)

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

How to Shrink a VirtualBox Virtual Machine and Free Up Disk Space (9)

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.

How to Shrink a VirtualBox Virtual Machine and Free Up Disk Space (10)

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.

How to Shrink a VirtualBox Virtual Machine and Free Up Disk Space (11)

Select “root” in the recovery menu to boot to a root shell prompt.

How to Shrink a VirtualBox Virtual Machine and Free Up Disk Space (12)

Press “Enter” afterwards when “Press Enter for maintenance” appears on your screen. You’ll be given a terminal prompt.

How to Shrink a VirtualBox Virtual Machine and Free Up Disk Space (13)

At the command line, determine which virtual disk you want to zero by running the following command:

df

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.

How to Shrink a VirtualBox Virtual Machine and Free Up Disk Space (14)

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

How to Shrink a VirtualBox Virtual Machine and Free Up Disk Space (15)

When the zero process is finished, run the following command to shut down the virtual machine:

halt

How to Shrink a VirtualBox Virtual Machine and Free Up Disk Space (16)

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”.

How to Shrink a VirtualBox Virtual Machine and Free Up Disk Space (17)

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 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\VirtualBoxby default. If you installed VirtualBox to another directory, look there instead.

How to Shrink a VirtualBox Virtual Machine and Free Up Disk Space (18)

Open a Command Prompt window. To do this, open the Start menu, type cmdand press Enter.

How to Shrink a VirtualBox Virtual Machine and Free Up Disk Space (19)

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.

How to Shrink a VirtualBox Virtual Machine and Free Up Disk Space (20)

If you’re using the default path, it should look like the following:

cd "C:\Program Files\Oracle\VirtualBox"

How to Shrink a VirtualBox Virtual Machine and Free Up Disk Space (21)

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.

How to Shrink a VirtualBox Virtual Machine and Free Up Disk Space (22)

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

How to Shrink a VirtualBox Virtual Machine and Free Up Disk Space (23)

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 isC:\Users\chris\VirtualBox VMs\Windows 7\Windows 7.vdi .

How to Shrink a VirtualBox Virtual Machine and Free Up Disk Space (24)

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

How to Shrink a VirtualBox Virtual Machine and Free Up Disk Space (25)

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.

How to Shrink a VirtualBox Virtual Machine and Free Up Disk Space (26)

READ NEXT

  • 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

FAQs

How do I reduce the size of a virtual machine disk? ›

Shrinking the virtual disk
  1. 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: ...
  2. Click the Shrink tab.
  3. Select the drive you want to shrink.
  4. Click Prepare to Shrink, then follow the onscreen instructions.
Sep 15, 2022

How do I reduce virtual size? ›

How to reduce the size of a virtual disk?
  1. 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. ...
  2. Preparing the server with the boot ISO. ...
  3. Clone the disk. ...
  4. Set the new disk as the root.

How do I reduce the provisioned size in vmware? ›

You can try a Storage vMotion and select thin provisioning on the destination route:
  1. Right-click the virtual machine, and click Migrate.
  2. Click Change datastore.
  3. Click Next, and select a datastore that is not the same as the current datastore.
  4. From the dropdown, select the Thin Provision virtual disk format.

Why is my virtual machine so big? ›

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? ›

Procedure
  1. Select Window > Virtual Machine Library.
  2. Select a virtual machine in the Virtual Machine Library window and click Settings.
  3. In the Settings window, click the hard disk you want to resize.
  4. Use the Disk size slider to set the new size.
May 31, 2019

How do I resize a Windows virtual machine? ›

  1. Open the Azure portal.
  2. Open the page for the virtual machine.
  3. In the left menu, select Size.
  4. Pick a new size from the list of available sizes and then select Resize.
Apr 27, 2023

What is the disk limit for VirtualBox? ›

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? ›

How to extend the volume inside the VM
  1. Right-click the VM and select Connect. ...
  2. Enter DISKMGMT. ...
  3. Right-click the C: drive and select Extend Volume. ...
  4. Select the available amount of space by which you want to extend the volume. ...
  5. Check that you have selected the correct settings.
Jun 1, 2023

How much disk space for VirtualBox? ›

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.

How to change fixed size to dynamic VirtualBox? ›

How to Convert Between Fixed and Dynamic Disks in VirtualBox
  1. Step One: Locate the VBoxManage Command and Open a Command Prompt. ...
  2. Step Two: Locate the Path to the Disk You Want To Convert. ...
  3. Step Three: Convert the Virtual Disk. ...
  4. Step Four: Remove the Old Virtual Disk. ...
  5. Step Five: Rename the New Disk.
May 29, 2019

References

Top Articles
Latest Posts
Article information

Author: Kieth Sipes

Last Updated: 05/10/2023

Views: 5959

Rating: 4.7 / 5 (47 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Kieth Sipes

Birthday: 2001-04-14

Address: Suite 492 62479 Champlin Loop, South Catrice, MS 57271

Phone: +9663362133320

Job: District Sales Analyst

Hobby: Digital arts, Dance, Ghost hunting, Worldbuilding, Kayaking, Table tennis, 3D printing

Introduction: My name is Kieth Sipes, I am a zany, rich, courageous, powerful, faithful, jolly, excited person who loves writing and wants to share my knowledge and understanding with you.