How to Reclaim Space from Docker Desktop WSL2

Photo of John Richardson

John Richardson

Fullstack PHP developer

Cover image for article: How to Reclaim Space from Docker Desktop WSL2

Why is it taking so much space?

Docker Desktop stores container images, volumes, and build cache in a WSL2 virtual disk (VHDX) file. While this file grows automatically to accommodate new data, it doesn’t automatically shrink when space is freed, leading to unnecessarily large disk usage over time.

Reclaiming space

First, remove unused resources:

docker system prune 

This removes stopped containers, unused networks & dangling images (unused and unreferenced)

Add -f to skip confirmation prompts.

As mentioned above, this wont immediately free up space on your hard drive. You’ll need to compact the VHDX file first.

Compacting the VHDX

Run the following in PowerShell (as admin):

wsl --shutdown
optimize-vhd -Path "C:\Users\YourUsername\AppData\Local\Docker\wsl\data\ext4.vhdx" -Mode full

Once done, your diskspace should be available immediately. I got around 10G back.

Fixing Virtual Disk Errors

If you get “Virtual Disk System Limitation” errors, your disk is likely in sparse mode which conflicts with optimization. Disable sparse mode first:

fsutil sparse setflag "C:\Users\YourUsername\AppData\Local\Docker\wsl\data\ext4.vhdx" 0

An update for 2026: still not fixed

For those still reading this in 2026 and beyond, Microsoft still haven’t sorted this. The VHDX still grows and never shrinks on its own. The manual compact above is still the reliable fix, but a few things have changed that are worth knowing.

  • Sparse mode is now gated as unsafe. Since WSL 2.5.6 (April 2025), enabling it needs a --allow-unsafe flag. It went behind the gate after data corruption reports, so I’d leave it off for anything holding real container data. microsoft/WSL#10609
  • Most “sparse doesn’t shrink” complaints are a measurement trap. With sparse on, the space frees within seconds but the logical file size never changes, and that’s the number Explorer and dir show. Check “Size on disk” in Properties instead. microsoft/WSL#12103, Microsoft Q&A
  • optimize-vhd and diskpart refuse sparse files entirely. If your disk is sparse you have to convert it back first (the fsutil setflag ... 0 above), which re-inflates it on disk before you can compact. Budget the space for that.
  • You can skip fstrim now. On WSL 2.7+ the root filesystem mounts with discard, so deletes trim immediately. Straight from docker system prune to compact works fine. Running fstrim anyway does no harm.
  • The core request is still open. microsoft/WSL#4699 (asking for automatic reclamation) has been open since 2019 with no committed fix.
  • Docker’s newer single-disk layout has a fresh regression where prune, diskpart, fstrim and sparse all fail to reclaim, and the VHDX creeps up after every Docker Desktop update. docker/desktop-feedback#412

Short version: same manual compact, drop the fstrim step, leave sparse mode alone.

© John Richardson Development. All rights reserved.