Ask
30
@vhdx_vera ·

My WSL disk keeps growing and never gives the space back when I delete files — where has it gone and how do I reclaim it? Disk & storage

Laptop is nearly full and the culprit is clearly the WSL virtual disk. Inside the distro df -h says I am using about 12GB. On the Windows side the ext4.vhdx file is over 90GB.

I have deleted a lot inside Linux — old container images, build artefacts, a couple of huge datasets — and the Windows file did not shrink by a single byte.

What I would like to understand rather than just run a command someone pasted:

  1. Why does deleting inside the distro not shrink the file?
  2. Is that 78GB gap actually recoverable, or is some of it structural?
  3. Is there a way to make this stop happening rather than cleaning it up every few months?
5 answers Share
Report

Answering anonymously — a moderator will review it first.

  • @sparse_sun · 3h ago · 2 replies

    The gap is real and recoverable, and the reason is that two layers disagree about what "free" means.

    The .vhdx is a virtual disk that grows on demand. When Linux writes a file, the virtual disk asks Windows for more real blocks and the file gets bigger. When Linux deletes that file, it marks the blocks free in its own filesystem — it does not tell the virtual disk layer, and it certainly does not tell NTFS. From the outside those blocks still look occupied, because as far as Windows knows something was written there and nobody said otherwise.

    So the file records the high-water mark of everything you have ever stored, not what you are storing now. Your 90GB is the largest it ever needed to be, and the 12GB is what is actually in it.

    That is why it never shrinks on its own and why deleting more inside does nothing visible.

    28
    Share
    Reply

    Answering anonymously — a moderator will review it first.

    Report
    • @vhdx_vera · 3h ago

      High-water mark is exactly the model I was missing. I had assumed it was a leak of some kind rather than the design.

      13
      Share
      Reply

      Answering anonymously — a moderator will review it first.

      Report
  • @hyperv_hakan · 3h ago · 2 replies

    Reclaiming it is three steps and skipping the first is why people report that compaction "did nothing".

    1. Tell the filesystem to release the blocks. Inside the distro:

    sudo fstrim -av
    

    This is the step that communicates downward — it discards blocks Linux no longer uses so the virtual disk layer can see they are free. Without it, the compactor looks at the disk, sees data everywhere, and correctly reclaims nothing.

    2. Shut everything down, from PowerShell, because the file cannot be in use:

    wsl --shutdown
    

    Give it a few seconds. Docker Desktop must be closed too, or it holds its own distro open.

    3. Compact the file. diskpart as administrator:

    select vdisk file="C:\Users\<you>\AppData\Local\Packages\<distro>\LocalState\ext4.vhdx"
    attach vdisk readonly
    compact vdisk
    detach vdisk
    

    On a 90GB file holding 12GB, expect it to come back close to the real usage.

    25
    Share
    Reply

    Answering anonymously — a moderator will review it first.

    Report
    • @loopback_lior · 3h ago

      Worth adding: if the distro was installed through the Store the path is under Packages, but a distro imported with wsl --import sits wherever you put it. wsl --list --verbose plus a search for ext4.vhdx finds it if the usual path is not there.

      And back up before the first time you do this. The procedure is safe and well documented, but it is still a raw operation on the disk holding all your work.

      14
      Share
      Reply

      Answering anonymously — a moderator will review it first.

      Report
  • @loopback_lior · 36m ago

    For question 3 there is now a proper answer rather than a maintenance ritual: sparse mode.

    wsl --shutdown
    wsl --manage <distro> --set-sparse true
    

    With that on, the disk reclaims space as you delete, instead of only at the high-water mark. It is the setting you want on a laptop that builds containers, which from your description is you.

    Two honest caveats. There is a small performance cost, because the disk has to do the bookkeeping as it goes — most people never notice it, but it is not free. And it applies per distro, so if you have several you set it on each.

    Check wsl --help on your build for the exact spelling; this area has changed more than once and the flags moved.

    1
    Share
    Reply

    Answering anonymously — a moderator will review it first.

    Report