none
How to shrink a VHDX Hyper-V disk

    General discussion

  • I had a great deal of trouble shrinking a Hyper-V VHDX 250GB fixed disk that was 50% full. I was hoping for a best method for shrinking vhdx hd files.

    In the virtual machine I tried

    * defrag - shows 84% and decreases as I try more attempts

    * disk manager - shrink to smallest space and leave empty space unused

    In Hyper-v control on the host machine I tried

    * edit disk - compact

    * edit disk - convert -failed

    * edit disk - copy to new file and then tried all the above unsuccessfully

    On the host machine I tried

    * powershell Resize-vhd in many iterations but all failed

    Finally, I used Paragon Hard Disk Manager to create a HyperV image backup to a .vhdx. then I used Hyper-V to convert it to a expanding hard disk, and the new hard drive changed from 250GB to 100GB.

    Thanks!

    Wednesday, March 8, 2017 10:25 PM

All replies

  • Hi,

    A fixed size VHDX file is by design a fixed size and can not be shrunk.

    Dynamically expanding VHDX files can.

    The only way you could achieve this is to use dynamically expanding VHDX files.

    Thursday, April 20, 2017 4:15 PM
  • Hi Native Kid,

    This is how I shrink my VHDX files

    # Run in an elevated prompt
    
    $FilePath = "D:\Hyper-V\Virtual Hard Disks\Server01.vhdx"
    
    Mount-VHD -Path $FilePath -ReadOnly
    Optimize-VHD -Path $FilePath -Mode Full # or Quick
    Dismount-VHD -Path $FilePath
    REMARK: This will only work for Dynamic Disks (Missed the fixed disk part :-))


    • Edited by Rene5380 Wednesday, May 31, 2017 6:44 AM
    Wednesday, May 31, 2017 6:03 AM
  • This still does not get rid of (or reduce) the unallocated drive space inside the VHDX....

    Randall

    Tuesday, June 5, 2018 7:12 PM
  • In order to reduce the physical size consumed by a FIXED VHDX, you must first shrink the partition from inside the VM. Disk Management should be able to help you. Once that's done, then you can use Resize-VHD.

    More info and how-tos: https://www.altaro.com/hyper-v/shrink-hyper-v-virtual-disk-vhd-vhdx/


    Eric Siron
    Altaro Hyper-V Blog
    I am an independent contributor, not an Altaro employee. I accept all responsibility for the content of my posts. You accept all responsibility for any actions that you take based on the content of my posts.

    Tuesday, June 5, 2018 8:46 PM
  • Hi Eric,

    First, let me say thank you for your post on this topic, and many others...you have a ton of great info on Hyper-V. It seems whenever I've searched from some question on VMs, your posts come up more often than not, right at the top!

    That said, I do have some feedback for you on this specific post.

    I followed your directions, but it still did not reduce the .vdhx file size on the host.

    Your post states the steps are:

    1. Shrink partition in Guest and leave space unallocated
    2. Use Resize-VHD
    3. ADDITIONAL STEP I HAD TO DO: Run Compact Disk in the Hyper-V Manager for the .vdhx in question...only then did it actually reduce the size of the file size.

    Note using Get-VHD shows the .vhdx in question is of type Dynamic, but still showed (after creating 300GB of unallocated space in the guest disk), that the size and MinimumSize were equal. I see that in your screen shot in your post as well, to which you state: " In order to make this disk eligible for shrinking, the MinimumSize needs to be smaller than the Size." But you don't state why these would be the same and what to do if they are, even though it doesn't seem that they should be the same, given all the unallocated space in the guest disk. It would be nice to have that issue addressed in your post.

    Even after Compacting, GET-VHD still shows the same numbers for Size and MinimumSize...not sure why, and they are about 250GB greater than the FileSize value.

    Friday, August 31, 2018 3:54 PM
  • The referenced article is not about compacting a VHDX. It says so very early on. "Shrink" and "compact" are not the same operation. The resize operations ("shrink" in this case) are about changing how much space is made visible to the contained filesystem; "compact" is about changing how much space is physically consumed by a dynamically-expanding disk without affecting the amount visible to the contained file system(s). A resize operation will not always change the physical space consumed by a dynamically-expanding VHDX.

    The "MinimumSize" field is determined by the extent of space allocated by file system(s) inside the VHDX. The "Size" field is the configured size of the VHDX -- in other words, what it reports to the contained file system as available space. The "FileSize" field is the actual consumed space.

    The "FileSize" field is the only field that would be affected by a compact operation.

    The MinimumSize and Size field will be identical when all of the space inside the VHDX is allocated for use by the guest file system. That is explained in the article in the sentence immediately preceding the one that you quoted. The entirety of the following section is devoted to explaining what to do about it, using three different techniques, even. In short, you must reduce the amount of space allocated by the contained file system(s).


    Eric Siron
    Altaro Hyper-V Blog
    I am an independent contributor, not an Altaro employee. I accept all responsibility for the content of my posts. You accept all responsibility for any actions that you take based on the content of my posts.

    Friday, August 31, 2018 4:24 PM