WSL2: The Expanding Ghost (and How to get rid of it)

You know something’s off when your disk space vanishes like socks in a dryer, and nothing you delete brings it back.


If you’re using WSL2 on Windows, congrats — you’re probably hosting a quietly expanding ghost.

Yes. Even after clearing caches. Even after purging Docker. The virtual disk just… stays large.

The Prime Suspect: Docker’s Build Cache


If you’re running Docker inside WSL, this one’s for you.

Docker build layers stack up like geological sediment:

  • Unpruned build layers quietly accumulate
  • Each new image leaves a little trail behind
  • Over time, this becomes a landfill of gigabytes

One day I checked. My Docker cache was 80 GB…. Eighty!

“But I Deleted Stuff!” – What’s Actually Happening


Yes, you did. Here’s why it didn’t help:

  • Linux: “I marked the space as free.”
  • Windows: “Cool. I’m keeping the file at full size anyway.”
  • You: “Why is my disk still full?”
  • WSL: [unused bytes haunting your SSD]

WSL2 stores its entire Linux filesystem in a .vhdx file — a virtual disk.

Virtual disks aren’t mind readers. They act like physical hard drives: when you delete something inside, the data is logically gone — but the space isn’t reclaimed unless you explicitly clean it up from the outside.

  • It grows as you add stuff
  • It doesn’t shrink when you delete stuff

So even when Linux thinks it’s tidy. To Windows, the file is still big because no one came in with a vacuum. So it just… stays big. A quiet monument to every container, cache, and careless npm install.

The Fix: Manually Compact the Disk


You can get rid of the disk ghost — but you need special tools.
Here’s how to make the virtual disk reflect reality.

It’s always worked for me — but I follow one rule:

⚠️ Back up your stuff first. No backup, no pity.

Step-by-step (Windows 11)

  1. Open Command Prompt or PowerShell as Administrator
  2. Shut down your WSLs
    • wsl --shutdown
  3. Run:

(Adjust the path to match your setup. If you use the default settings, this works as-is.)

diskpart
select vdisk file="C:\Linux\ext4.vhdx"
attach vdisk readonly
compact vdisk
detach vdisk
exit

This can take a while depending on your ghost size — go make tea. Or refactor something.

When WSL starts again, the file will finally match the real size of your Linux world.

Memory ghost returned successfully to the realm of unused bytes.