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