How to manually shrink WSL2 virtual disk files and reclaim Windows storage
WSL2 stores each Linux distribution inside a dynamically expanding virtual disk file (.vhdx) on the Windows C: drive, which grows automatically but never shrinks on its own even after files are deleted inside Linux. The freed space remains within the .vhdx container until the file is manually compacted, meaning Windows does not reclaim it automatically. To compact the disk, users must first fully shut down WSL2 using 'wsl --shutdown', then run the Optimize-VHD PowerShell command on Windows Pro or Enterprise, or use diskpart on Windows Home where Hyper-V is unavailable. For compaction to recover meaningful space, users should first clean up unused packages, logs, and Docker layers from within the Linux environment before shutting down and running the compaction process. Docker Desktop users face the same issue but their virtual disk resides in a separate folder, and the same manual compaction steps apply.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.

Discussion (0)
Log in to join the discussion and vote.
Log in