Deleting files inside WSL frees nothing on Windows until you compact it
The Linux disk is a sparse file that only ever grows. How to see the real size, and the supported ways to give the space back.
Delete twenty gigabytes inside a WSL distribution, check Windows, and nothing has changed. This is not a bug and it is the single most common Windows storage question among developers.
Why it happens
A WSL distribution lives in a virtual hard disk, ext4.vhdx. It is a sparse file: it starts small and grows as the guest writes. When the guest deletes a file, the space is freed inside the Linux filesystem, and the vhdx keeps the blocks it has already claimed. From Windows' point of view nothing was freed, because nothing was.
Finding the disks
Get-ChildItem $env:LOCALAPPDATA\Packages -Recurse -Filter ext4.vhdx -ErrorAction SilentlyContinue |
Select-Object @{n='GB';e={[math]::Round($_.Length/1GB,2)}}, FullName
# Docker Desktop keeps its own, in the same shape
Get-ChildItem $env:LOCALAPPDATA\Docker -Recurse -Filter *.vhdx -ErrorAction SilentlyContinue |
Select-Object @{n='GB';e={[math]::Round($_.Length/1GB,2)}}, FullNameClear inside first, then compact
Compacting only reclaims blocks the guest is no longer using, so the order matters:
- Inside the distribution, remove what you mean to: build output, package caches, old images.
- For Docker,
docker system pruneand, deliberately,docker volume pruneif the volumes are genuinely finished with. - Back in Windows, shut WSL down completely with
wsl --shutdown. - Compact the disk.
wsl --shutdown
# Newer builds can do it directly
wsl --manage <DistroName> --set-sparse trueOn builds without that option, diskpart does the same job:
diskpart
select vdisk file="C:\path\to\ext4.vhdx"
attach vdisk readonly
compact vdisk
detach vdisk
exitStopping it growing so fast
- Keep build output out of the guest home where you can, or clear it as part of the build.
- Prune Docker regularly. Build cache is the fastest growing part and the safest to remove.
- Watch package caches. apt, npm and pip caches inside the guest are invisible from Windows and count against the same disk.
- Remove distributions you finished with.
wsl --unregister <name>deletes the disk outright.
The same shape elsewhere
Any virtual disk behaves this way, which is why a virtual machine keeps growing even when the guest is empty describes the identical mechanism for Parallels and VMware. The Windows specific part is only that WSL makes it easy to fill without noticing.
Common questions
Why does deleting files in WSL not free space in Windows?
Because the Linux filesystem lives in a sparse virtual disk that only grows. Deleting inside the guest frees space within that filesystem, and the vhdx keeps the blocks it already claimed until it is compacted.
How do I shrink a WSL disk?
Clear what you mean to inside the distribution, run wsl --shutdown, then compact the vhdx, either with wsl --manage --set-sparse on newer builds or with diskpart's compact vdisk.
Where is the WSL disk stored?
Under %LOCALAPPDATA%\Packages, in a folder named for the distribution, as ext4.vhdx. Docker Desktop keeps its own vhdx under %LOCALAPPDATA%\Docker.
Is it safe to compact a WSL disk?
Yes, with the distribution shut down first. Compacting a disk that is still attached is one of the few ways to corrupt the filesystem inside it.