How to Fix: Compact VHD (Virtualbox, Hyper-V, etc)

Dennis Faas's picture

Infopackets Reader Bill G. writes:

" Dear Dennis,

My question is about how to compact a VHD file (virtual disk). I recently virtualized an old Windows 2016 server and am using Virtualbox as my hypervisor. In the process of updating and configuring the virtual machine, I created 3 snapshots (live backups) in case I needed to undo changes. The .VHD is the Server 2016 C Drive and was originally only 60GB - however, I noticed that the 'snapshots' folder has grown to a massive 168GB! I went into virtual machine's snapshot window and deleted all the snapshots, hoping to free up space on the disk that stores the virtual machine. That's when Virtualbox told me it would take 30+ minutes to merge the snapshots with the original VHD. Now my Windows Server C drive .VHD files is a whopping 223GB! I can't seem to find an answer on how to compact a .VHD in virtualbox - and on top of it, there doesn't seem to be a native function in Virtualbox to achieve this. The posts I've come across say to power down the virtual machine, defrag the .VHD and then delete the empty space with 0's using 'sdelete.exe'. Frankly I'm worried all this messing about will corrupt the VHD file. Do you know a better way to compact a .VHD? "

My response:

I asked Bill if he would like me to connect to his machine using my remote desktop support service in order have a closer look at the issue, and he agreed.

Below I will discuss my findings.

How to Fix: Compact VHD (Virtualbox, Hyper-V, etc)

This solution uses a general approach on how to compact a .VHD regardless of which hypervisor you use - whether it's Virtualbox, Hyper-V, VMWare Workstation, or another.

  1. First, make a backup of the entire virtual machine folder before proceeding. Whenever you modify a .VHD file that is part of a virtual machine, there is a possibility that something could go wrong and render the virtual machine useless. To make a backup, you will need to power down the virtual machine if it is running, then drag and drop the virtual machine folder onto another drive.
     
  2. Optional: using the hypervisor, delete as many snapshots as you want in order to free up space. Keep in mind that these snapshots may need to be merged with the original .VHD file later (as with Virtualbox), and require a substantial amount of disk space to process. If you run out of disk space while merging data, it could corrupt the virtal machine. If this happens, you can delete the old virtual machine and re-import the backed up copy.
     
  3. Once the snapshots have been cleared and the virtual machine is powered off, you are ready to compact the .VHD file. For this task, I will be using the command line and 'diskpart', which is built into Windows. To do so: click Start, then type in "CMD" (no quotes); wait for "CMD.EXE" or "Command Prompt" to appear in the list, then right click and select "Run As Administrator".
     
  4. Highlight the next below using your mouse:

    (echo select vdisk file="c:\path-to-vitual-machine\server-c.vhd"
    echo attach vdisk readonly
    echo compact vdisk
    echo detach vdisk ) | diskpart
     
  5. Right click the above text and select "Copy" from the dialogue menu.
     
  6. Launch Notepad (Start -> Notepad) and past the text you copied in Step #4.
     
  7. Modify the first line that says: select vdisk file="c:\path-to-vitual-machine\server-c.vhd" and change the path to point to your .VHD file.
     
  8. Select all the text in Notepad, then copy it to your clipboard. Next, go to the command prompt you opened in Step #3 and right click in the middle of the window. The text you copied from Notepad should be output to the command line. If you were successful, you will see output indicating that the .VHD file has been compacted. If there is an error, it's most likely because the path to the .VHD file is invalid and you will need start from Step #7 again.
     
  9. At this point, your .VHD file should be compacted; you can compare it with the backed up .VHD to see show much space has been reclaimed. If it did not shrink as much as you anticipated, you may need to attach the .VHD to the host operating system and defrag it, then try compacting it again.
     
  10. Assuming everything is good to go, the next step is to start up the virtual machine. If all goes well, the virtual machine should start normally. If you encounter errors, you can try the process again to see if that makes a difference. If not, you will need to re-import the backed up copy of the virtual machine. You can also contact me for remote desktop support, and I will manage the entire thing for you (described next).

I hope that helps!

Additional 1-on-1 Support: From Dennis

If all of this is over your head, or if you need help compacting your virtual machine - or any other virtual machine issue for that matter - you are welcome to contact me for additional support. Simply contact me, briefly describing the issue and I will get back to you as soon as possible.

About the author: Dennis Faas

Dennis Faas is the owner and operator of Infopackets.com. With over 30 years of computing experience, Dennis' areas of expertise are a broad range and include PC hardware, Microsoft Windows, Linux, network administration, and virtualization. Dennis holds a Bachelors degree in Computer Science (1999) and has authored 6 books on the topics of MS Windows and PC Security. If you like the advice you received on this page, please up-vote / Like this page and share it with friends. For technical support inquiries, Dennis can be reached via Live chat online this site using the Zopim Chat service (currently located at the bottom left of the screen); optionally, you can contact Dennis through the website contact form.

Rate this article: 
Average: 5 (4 votes)