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
    Join Date : Jul 2015
    Posts : 3,808
    10 Pro

    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:	48 
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
    Join Date : Jul 2015
    Hasselt, Belgium
    Posts : 171
    Windows 10 Pro

    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
    Join Date : Jul 2015
    Posts : 3,808
    10 Pro

    Quote Originally Posted by Rudy Mas 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
    Join Date : Jul 2015
    Hasselt, Belgium
    Posts : 171
    Windows 10 Pro

    Quote Originally Posted by halasz 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
    Join Date : Jul 2015
    Posts : 3,808
    10 Pro

    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.    30 Aug 2015 #7
    Join Date : Jun 2015
    Posts : 1,929
    Windows 3.1 to Windows 10

    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
  9.    28 Sep 2017 #9
    Join Date : Jan 2016
    Posts : 2
    Windows 10

    Quote Originally Posted by mfaklis View Post
    The solution from Rudy Mas worked as I expected.

    I have not test kuhi's solution yet.
    Everything except for the outdated Sdelete step. That is everywhere on the net but is not necessary now if it was before. It would take more than 24 hours on my setup to complete sdelete. Using the hypervisor administrator in 2016 server, I skipped the sdelete step, or zeroing free space. I just ran disk compact after deleting 150gb without zeroing anything and the size on disk was compacted by Hypervisor administrator to the 38GB's stored on disk and the virtual disk size unchanged.
      My ComputerSystem Spec
  10.    29 Sep 2017 #10
    Join Date : Jul 2015
    Posts : 3,808
    10 Pro

    Quote Originally Posted by sticker592 View Post
    Everything except for the outdated Sdelete step
    Still need to zero free space if it isn't NTFS though.
      My ComputerSystem Spec

 


Similar Threads
Thread Forum
DVD/CD SATA drive not reading disks
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...
Drivers and Hardware
Using A Friends Unused Product Key & Driver Question
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...
Installation and Upgrade
How do I burn disks
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.
General Support
Solved Update to 10 for unused 7/8 licenses?
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...
General Support
I lost 2 Hard Disks after updated from Build 9860 to 9879
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...
Drivers and Hardware
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 06:02.
Find Us
Twitter Facebook Google+ Ten Forums iOS App Ten Forums Android App



Windows 10 Forums