To reclaim Docker disk space, run docker system prune -a --volumes to remove unused images, containers, networks, and volumes. To shrink the WSL2 virtual disk after that, shut WSL down with wsl --shutdown and compact its ext4.vhdx file. Docker Desktop on Windows stores everything inside a WSL2 backing disk that grows but does not automatically shrink, which is why it balloons.
TL;DR
docker system prune -a --volumesfrees unused images, build cache, and volumes.docker builder pruneclears just the build cache if you want to keep images.- The WSL2
.vhdxgrows but never auto-shrinks; you must compact it manually. - Run
wsl --shutdownfirst, then compact withdiskpartorOptimize-VHD. - Pruning only touches unused objects; running containers and tagged images you use are safe.
How do I clear unused Docker data?
Open a terminal and run:
docker system prune -a --volumes
This removes all stopped containers, all networks not used by a container, all dangling and unused images (the -a flag), the build cache, and all unused volumes (--volumes). Docker prints how much it reclaimed. To see usage first, run docker system df, which breaks down space by images, containers, local volumes, and build cache.
If you only want to clear the build cache, which is often the largest hidden consumer, run docker builder prune. To target dangling images alone, use docker image prune.
Why does Docker Desktop disk usage keep growing?
Docker Desktop with the WSL2 backend keeps its data inside a virtual hard disk, typically at %LOCALAPPDATA%\Docker\wsl\disk\docker_data.vhdx (path varies by version). This .vhdx file is a sparse, dynamically expanding disk: it grows as you pull images and build, but deleting data inside it does not shrink the file on your real drive. So even after docker system prune, Windows still shows the disk as full until you compact the vhdx.
How do I shrink the WSL2 vhdx file?
First free the data inside (prune Docker, or run sudo apt clean and delete large files in your own distro). Then shut everything down:
wsl --shutdown
If you have Hyper-V tools (Windows Pro/Enterprise), compact in PowerShell as Administrator:
Optimize-VHD -Path "C:\path\to\ext4.vhdx" -Mode Full
On Windows Home, use diskpart instead:
diskpart
select vdisk file="C:\path\to\ext4.vhdx"
attach vdisk readonly
compact vdisk
detach vdisk
exit
Find your distro's vhdx under %LOCALAPPDATA%\Packages\ (search for ext4.vhdx), and Docker's under %LOCALAPPDATA%\Docker\wsl. Always close Docker Desktop and run wsl --shutdown before compacting, or the file will be locked.
What the OS does natively, and where it stops
Windows and WSL natively let you list usage (docker system df, wsl --list --verbose), prune unused objects, and shut down the WSL VM. Docker's prune commands are the supported, safe way to reclaim space inside the disk.
Where it stops: neither Docker nor WSL automatically returns the freed space to your physical drive. The vhdx stays at its high-water mark until you manually compact it, and there is no scheduled task that does this for you.
What this cannot do / leave alone
docker system prune -a --volumes deletes unused volumes, which may contain database data for stopped projects you still care about. Run docker volume ls first and back up anything important. Do not delete the .vhdx files by hand while keeping the distro registered, as that corrupts WSL; if you want a clean slate, use wsl --unregister <distro> instead, which permanently erases that distro. Leave the system distro docker-desktop alone, and never edit the vhdx with non-Microsoft tools while it is attached.
FAQ
Will docker system prune delete images I am using?
No. It removes only unused objects: stopped containers, dangling images, and (with -a) images not referenced by any container. Running containers and the images they use are never removed.
Why is my disk still full after pruning Docker?
Because the WSL2 .vhdx does not shrink automatically. Pruning frees space inside the virtual disk, but you must run wsl --shutdown and compact the vhdx with Optimize-VHD or diskpart to return that space to Windows.
Is it safe to compact the WSL vhdx?
Yes, when done correctly: close Docker Desktop, run wsl --shutdown, then compact. Compacting only removes empty space and does not touch your files. The risk is compacting while the disk is in use, so always shut down WSL first.
If the storage crunch is on your iPhone instead, Cleanor for iPhone clears the photos and clutter filling it up.