VHDX size reduction

%3CLINGO-SUB%20id%3D%22lingo-sub-2320679%22%20slang%3D%22en-US%22%3EVHDX%20size%20reduction%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2320679%22%20slang%3D%22en-US%22%3E%3CP%3EI%20still%20have%20hopes.%20The%20dynamic%20VHDX%20container%20is%20so%20useful%20however%20it%20has%20a%20big%20downfall%20as%20it%20cannot%20shrink.%3C%2FP%3E%3CP%3EAdding%20data%20into%20a%20dynamic%20VHDX%20keeps%20it%20expanding%20in%20size%20even%20if%20you%20delete%20data%20from%20it.%3C%2FP%3E%3CP%3EThis%20often%20gives%20ridiculous%20results%20when%20there%20is%20a%20500%20GB%20VHDX%20container%20containing%20only%20serverl%20GBs%20of%20data.%3C%2FP%3E%3CP%3EI%20know%20that%20shrinking%20is%20possible%20with%20an%20awkward%20procedure%20of%20taking%20the%20VHDX%20offline%2C%20defragment%20it%2C%20null%20free%20space%20(with%20external%20tool%20as%20Windows%20cannot%20do%20that)%20and%20then%20use%20a%20Poweshell%20command%20and%20hope%20the%20size%20gets%20reduced.%3C%2FP%3E%3CP%3EBut%20we%20have%202021%20now%20and%20there%20should%20be%20a%20way%20of%20self-shrinking%20dynamic%20VHDX%20that%20never%20occupis%20more%20space%20than%20data%20in%20it%20requires.%20And%20if%20data%20is%20reduced%2C%20it%20shrinks%20without%20any%20required%20interation.%3C%2FP%3E%3CP%3EOtherwise%20this%20'dynamic'%20VHDX%20is%20just%20a%20sad%20joke.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2320679%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3Edisk%20shrink%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Edynamic%20virtual%20disk%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Evhdx%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EWindows%20Server%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2328427%22%20slang%3D%22en-US%22%3ERe%3A%20VHDX%20size%20reduction%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2328427%22%20slang%3D%22en-US%22%3EAs%20I'm%20sure%20you%20know%2C%20fundamentally%2C%20the%20VHD%20container%20is%20a%20simple%20abstraction%20of%20a%20real%20hard%20drive%20in%20a%20container.%20Since%20the%20underlying%20operating%20system%20will%20write%20to%20that%20disk%20in%20a%20way%20to%20minimize%20fragmentation%2C%20the%20overall%20size%20of%20a%20dynamic%20VHD%20is%20going%20to%20increase%20over%20time%20with%20the%20more%20writes%20you%20have.%20Additionally%2C%20the%20default%20block%20size%20for%20VHDX%20was%20increased%2C%20which%20makes%20dynamic%20VHDXs%20use%20even%20more%20space%20than%20before.%20You%20can%20however%2C%20build%2Fconvert%20VHDXs%20to%20as%20low%20as%201MB%20block%20size%2C%20which%20will%20keep%20them%20slightly%20smaller%20and%20closer%20to%20the%20real%20size%2C%20at%20the%20expensive%20of%20performance.%3CBR%20%2F%3E%3CBR%20%2F%3EI've%20been%20using%20Hyper-V%20for%20a%20solid%20decade%20now%2C%20and%20I've%20never%20had%20a%20need%20to%20use%20dynamic%20VHDs%20on%20production%20servers.%20But%20in%20my%20homelab%20I%20do%2C%20and%20after%20messing%20about%20with%20sdelete%2C%20powershell%20scripts%2C%20etc.%20for%20years%2C%20I%20realized%20I%20was%20wasting%20my%20time%2C%20and%20hitting%20the%20Compact%20option%20in%20the%20Hyper-V%20Manager%20on%20occasion%20was%20good%20enough%20for%20my%20typical%20use%20case.%3CBR%20%2F%3E%3CBR%20%2F%3EIn%20this%20case%20of%20500GB%20VHDs%20where%20there's%20only%20a%20few%20GB%20of%20data%2C%20I%20think%20you%20have%20a%20few%20options.%20Perhaps%20all%20you%20need%20is%20a%20regularly%20scheduled%20nightly%20Powershell%20task%20that%20will%20shut%20down%20the%20VM%20(if%20turned%20on)%2C%20run%20Optimize-VHD%20and%20then%20turn%20it%20back%20on%2C%20or%20something%20similar.%20Or%20if%20you're%20doing%20a%20lot%20of%20writes%20to%20the%20VHD%2C%20you%20might%20want%20to%20consider%20mapping%20a%20drive%20letter%20or%20mount%20in%20a%20directory%20a%20SMB%20path%20to%20the%20host%2C%20and%20doing%20the%20writes%20there%20instead%20to%20avoid%20inflating%20the%20size.%3C%2FLINGO-BODY%3E
New Contributor

I still have hopes. The dynamic VHDX container is so useful however it has a big downfall as it cannot shrink.

Adding data into a dynamic VHDX keeps it expanding in size even if you delete data from it.

This often gives ridiculous results when there is a 500 GB VHDX container containing only serverl GBs of data.

I know that shrinking is possible with an awkward procedure of taking the VHDX offline, defragment it, null free space (with external tool as Windows cannot do that) and then use a Poweshell command and hope the size gets reduced.

But we have 2021 now and there should be a way of self-shrinking dynamic VHDX that never occupis more space than data in it requires. And if data is reduced, it shrinks without any required interation.

Otherwise this 'dynamic' VHDX is just a sad joke.

1 Reply
As I'm sure you know, fundamentally, the VHD container is a simple abstraction of a real hard drive in a container. Since the underlying operating system will write to that disk in a way to minimize fragmentation, the overall size of a dynamic VHD is going to increase over time with the more writes you have. Additionally, the default block size for VHDX was increased, which makes dynamic VHDXs use even more space than before. You can however, build/convert VHDXs to as low as 1MB block size, which will keep them slightly smaller and closer to the real size, at the expensive of performance.

I've been using Hyper-V for a solid decade now, and I've never had a need to use dynamic VHDs on production servers. But in my homelab I do, and after messing about with sdelete, powershell scripts, etc. for years, I realized I was wasting my time, and hitting the Compact option in the Hyper-V Manager on occasion was good enough for my typical use case.

In this case of 500GB VHDs where there's only a few GB of data, I think you have a few options. Perhaps all you need is a regularly scheduled nightly Powershell task that will shut down the VM (if turned on), run Optimize-VHD and then turn it back on, or something similar. Or if you're doing a lot of writes to the VHD, you might want to consider mapping a drive letter or mount in a directory a SMB path to the host, and doing the writes there instead to avoid inflating the size.
www.000webhost.com