Windows 10: How to release unused space in VHDX disks? Solved

  1.    27 Aug 2015 #1

    How to release unused space in VHDX disks?


    Before I learned that VHDX expanding disks do not release space, many of these have swelled way beyond what is used of needed because I did a defrag. Defrag will use temporary space to store files parts being defragged. I expected defrag to make fragmented files use contiguous space and to move all of the files to the beginning of the VHDX disk. To emphasize my ignorance I expected defrag to truncate to unused space at the end of the disk.

    What really happens is that defrag extends expanding VHDX disks as a temporary area to make files contiguous and as temporary space to squeeze out embedded free space. The new extensions are never released to the host physical disk. Defragging a VHDX extended space disk will result in allocating significant more unused physical space. As an example a 100gb fragmented expanding VHDX disk once defragged may end up actually expanding to over 300gb of real space.

    A old version of Virtual PC came with a compactor.ISO utility that when run against a VHD, would squeeze all unused space to the end of the VHD disk. You could then truncate the trailing free space leaving your VHD file physically smaller. Microsoft provides no tool (I know of) that will accomplish the physical shrinking of VHDX disks. The problem may apply to VHD disks as well, but I am now using VHDX virtual disks.

    Does anyone have the necessary expertise of PowerShell to script making VHDX disk files contiguous, squeezing all the unused space to the end of the VHDX disk, and then truncating the trailing free space to physically shrink virtual disks.
      My ComputerSystem Spec

  2.    30 Aug 2015 #2

    Just take the edit disk option from the Hyper-V management console, select your disk and then the option compact.

    Click image for larger version. 

Name:	Edit VHD.png 
Views:	25 
Size:	106.1 KB 
ID:	34701

    If you want to use powershell instead of the UI you could do this:
    Code:
    Mount-VHD <enter path to vhdx file> -ReadOnly
    Optimize-VHD <enter path to vhdx file> -Mode full 
    Dismount-VHD <enter path to vhdx file>
    If your vhdx is not NTFS (Linux for example) you need to zero out free space first. With NTFS (Windows) you need not.
      My ComputerSystem Spec

  3.    30 Aug 2015 #3

    Before you do the above, use sdelete (SDelete) to zero out all the unused bits on the virtual disk. This will give you a smaller file than when you just do a compact straight away.

    sdelete has to be run inside the guest OS, of course.
      My ComputerSystem Spec

  4.    30 Aug 2015 #4

    Rudy Mas said: View Post
    Before you do the above, use sdelete (SDelete) to zero out all the unused bits on the virtual disk. This will give you a smaller file than when you just do a compact straight away.

    sdelete has to be run inside the guest OS, of course.
    You do not need to do this with NTFS vhdx - just defrag and compact. Compacting a Dynamically Expanding virtual hard disk in Windows Server 2012 - Ask the Core Team - Site Home - TechNet Blogs
      My ComputerSystem Spec

  5.    30 Aug 2015 #5

    halasz said: View Post
    Thanks for the information!

    I now use VMware player, and I still need to do this there, even when using NTFS.

    A while back, when I did use Hyper-V, I noticed that running sdelete inside the guest OS still did a better job than when I only ran compact on it. Maybe it's gotten better? At least, thanks for mentioning this! It's again one thing less to do.
      My ComputerSystem Spec

  6.    30 Aug 2015 #6

    I have 2 VM's running Windows 10 Pro (both 10240 and both compacted and minimised as best I could)

    Hyper-V VM running on Windows 10 host without Office is 5.45GB according to Windows file explorer and the vhdx is 8.16GB

    VMware Fusion (latest version) VM running on OSX host with Office 2016 is 8.35GB according to Windows file explorer and the vmdk is 11.1 GB

    To be honest I don't really trust any of these numbers as whenever I look in a different place I get a different answer...How much disk am I using? - Windows 10 Forums

    I didn't run sdelete on VMware (actually I forgot, thanks for reminding me). I will do and see if it helps any.
      My ComputerSystem Spec


  7. Posts : 1,841
    Windows 3.1 to Windows 10
       30 Aug 2015 #7

    mount and defrag (optimize) vhd
    then type
    diskpart
    select vdisk file="X:\NAME.vhd"
    Compact vdisk
    exit
      My ComputerSystem Spec

  8.    31 Aug 2015 #8

    This worked.


    The solution from Rudy Mas worked as I expected.

    I have not test kuhi's solution yet.
      My ComputerSystem Spec


 

Related Threads
DVD/CD SATA drive not reading disks in Drivers and Hardware
Since upgrading my SATA DVD/CD RW will not work. I insert a disk the light come on showing it is reading and than the drive opens and the pop up box appears stating "Insert Disk" . So I cannot install any of my software. I have read that a lot of...
Hello I am currently running Windows Vista on my computer, but would like to upgrade to Windows 10. I have a few questions. My friend, who has a Windows 7-running computer, is willing to give me their product activation key for their free...
How do I burn disks in General Support
My DVD reads fine but I don't know how to burn disks. I have tried sharing in the window for the drive and there is a burn place there, but it doesn't do anything.
Read update FAQ, but... Bought 4 Win8 licenses when Win8 was released. Tried 1 (as Win8.1) but keep getting BSOD when my hdd would "sleep", so bailed (without activation?) and bought Win7. I also bought an additional Win7 license then, "just in...
Updated to build 9879 with no problems from build 9860. However, I powered down and restarted the next morning to find "Disk Boot Failure - Insert system disk". Whatever I tried to see inside the disk failed because it wasn't recognised in...
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

Designer Media Ltd
All times are GMT -5. The time now is 15:21.
Find Us