How to Reclaim Unused Space in Dynamic VHDX in Windows Server Hyper-V? #HyperV

2 Min. Read

The other day I had a request to send a Virtual Machine over the continent.

Well, this is very easy, but what if the size of the VM is large and you have limited Internet bandwidth!

So without further ado, let’s jump into the scenario:

The guest OS is Windows Server 2012 R2 with 50GB dynamic VHDX. This VM is used by the Dev department for Webserver testing purposes. They do copy large files, delete, etc…

The VM is Generation 1 and has an IDE attached to VHDX.

If we look inside the VM we can see that ~15GB of space is being used which is basically the OS bits, updates, and small web application:

Optimize-Volume01

However, if we look at the host, we can see that this VM is occupying 45GB of disk space, this is odd Disappointed smile

Optimize-Volume02

In Windows Server 2012 and above, Microsoft introduced the TRIM/UNMAP feature. When a large file is deleted from the file system of a VM guest operating system, the guest operating system sends a file delete request to the virtual machine’s virtual hard disk file. The VM’s VHD or VHDX file tunnels the UNMAP request to the class driver stack of the Windows Hyper-V host. So in the scenario above, the disk would initially expand from 15GB to 45GB but when the large files are deleted inside the guest, this would theoretically (automatically) be able to drop back down the VHDX file to ~15GB size.

Why I said theoretically because the hardware needs to specifically support this. So you’ll need to check with the storage vendor to know if this will work or not.

In my scenario, I am running the Virtual Machine locally on DAS storage, therefore the TRIMP/UNMAP feature won’t work automatically.

So how can we reclaim back the unused space?

With PowerShell, of course… Winking smile

Let’s fire up PowerShell within the guest OS and force UNMAP using the following cmdlet:

Optimize-Volume –DriveLetter C –ReTrim –Verbose

Optimize-Volume03

Shut down the VM and look again at the size of the dynamic VHDX file. It shrinks back to the original file size of ~15GB.

Optimize-Volume04

If you like to see TRIM/UNMAP on a Dell Compellent SAN in action, my fellow MVP Didier Van Hoye has recorded a screencast demo.

In conclusion, Trim requires trim-compatible hardware and is also requires Windows Server 2012 or later release on Hyper-V hosts and guests.

Update: Storage Spaces supports UNMAP/Thin provisioning, so if your Virtual Machines are stored on Storage Spaces volumes, you will get the benefit of UNMAP automatically without any trim-compatible hardware.

In the upcoming post, we will see how can we reclaim unused space in dynamic VHDX in the older guest OS.

Hope this helps.

Until then… enjoy your day!

Cheers,
/Charbel

Related Posts

Previous

Microsoft Announced The General Availability For Azure Site Recovery #Azure #HyperV #MSCloud

StarWind V2V Converter #StarWind

Next

4 thoughts on “How to Reclaim Unused Space in Dynamic VHDX in Windows Server Hyper-V? #HyperV”

Leave a comment...

  1. I ran the above PS command on one of my .vhdx file but it failed to reclaim the unused space. Is there any other PS commands that we can use to reclaim the unused space on a .vhdx file.

  2. What is the output you received after you ran the PS command above?
    Did you shutdown the guest OS afterwards?
    On what storage type the VM reside?

  3. Hi,
    Thanks for sharing this.

    Just to clarify – when you say shut down the guest OS. I assume once it’s booted back up again the reclaimed space will remain.

  4. Hello Dan, thanks for the comment!
    What I mentioned about shutting down the guest OS is to reclaim (reduce) the VHDX size, once the VM is booted back up again, the reclaimed space will remain (~15GB based on this example), and then it will grow as soon as you add/write more data.
    Hope it’s clear now!

Let me know what you think, or ask a question...

The content of this website is copyrighted from being plagiarized!

You can copy from the 'Code Blocks' in 'Black' by selecting the Code.

Please send your feedback to the author using this form for any 'Code' you like.

Thank you for visiting!