How to free up and automatically manage disk space for WSL

freecodecamp.org

65 points by twilight-code 4 days ago


Dylan16807 - 2 days ago

I think Optimize-VHD will do the same thing as messing with diskpart.

It's a shame you can't just turn on live TRIM support.

This kind of disk image is a bunch of multi-megabyte blocks of data, plus a list of where each block goes on the virtual disk. Implementations can support TRIM by deleting the block that's zeroed and moving the block at the end of the file into that spot. VirtualBox can do this, shrinking the file when the guest OS TRIMs, but Hyper-V can't.

jve - 2 days ago

Actually great intro for step-by-step powershell scripting.

I thought I didn't know about "Throw-And-Exit" command, but no, my powershell doesn't have that and google also doesn't know a thing, so wonder what's up with that.

And instead of manual confirmations, one can write script with ShouldProcess support to have support for builtin -Confirm/-WhatIf/-Force parameters. And it would actually be a great use case for that scheduled task to run with -Force parameter instead of changing code to strip out confirmations. https://learn.microsoft.com/en-us/powershell/scripting/learn...

acemarke - 2 days ago

I recently used https://github.com/okibcn/wslcompact to shrink a couple of WSL VHDX files, and it worked great.

Looking at the source, it seems to use a `wsl --export` option rather than `diskpart`.

Saavedro - 2 days ago

You also may need to run fstrim inside WSL to make all the free space actually compactible

Eridrus - 2 days ago

Someone should really make a utility to make this easy if Microsoft won't do it.

eviks - 2 days ago

By the way, is WSL1, which doesn't use vhds and so doesn't suffer from such issues (but has slowed io), effectively abandoned by MS?

rr808 - 2 days ago

I'm done with WSL. It just makes life too hard, dual disk is a problem, two network endpoints two firewalls, two different hypervisors you can choose from where some things expect one some the other.

I used to think a windows laptop would be better for hardware management, maybe it is but I just gave up and installed Linux. My life is so much easier.