Managing WSL Disk Space
Source: Dev.to
Shrinking WSL 2 VHDX Files
If you’ve been using WSL 2 for a while, you’ve probably noticed your C: drive slowly losing space (10 GB here, 50 GB there).
The culprit? The virtual hard‑disk files (*.vhdx) that back each distribution. They grow as you use them but never shrink on their own.
“I deleted 20 GB of files inside the distro, checked Windows disk space, and nothing changed.”
Below is a step‑by‑step guide to reclaim that space.
1. Where the VHDX files live
| Location | Typical path |
|---|---|
| Default (newer WSL) | %LOCALAPPDATA%\wsl\ |
| Store‑installed distros | %LOCALAPPDATA%\Packages\\LocalState\ |
| Custom locations | Whatever you gave to wsl --import |
To get the exact path for a specific distro, look in the registry:
HKCU\Software\Microsoft\Windows\CurrentVersion\Lxss\{GUID}\BasePath
The VHDX file is always named ext4.vhdx inside that base path.
List all VHDX sizes (PowerShell)
Get-ChildItem -Path "$env:LOCALAPPDATA\wsl" -Recurse -Filter "ext4.vhdx" |
Select-Object FullName,
@{N='SizeGB';E={[math]::Round($_.Length/1GB, 2)}}
2. Compare Linux usage vs. VHDX size
Inside the distro:
df -h /
If the VHDX is 45 GB but df shows only 20 GB used, you have ~25 GB that can be reclaimed.
3. Zero out free space (run fstrim)
The fstrim command tells the filesystem to discard blocks that are no longer in use, making them appear as zeroes to the VHDX compactor.
# As root (or with sudo)
sudo fstrim -av
Typical output:
/: 12.5 GiB (13421772800 bytes) trimmed on /dev/sdd
Alpine Linux note – Alpine’s BusyBox uses a simpler syntax:
sudo fstrim -v /
4. Shut down WSL (so the VHDX isn’t locked)
wsl --shutdown
Wait a few seconds for the VM to fully stop.
5. Compact the VHDX
You have two options, depending on whether Hyper‑V is available.
Option A – Optimize‑VHD (requires Hyper‑V, Windows Pro/Enterprise)
# Run as Administrator
Optimize-VHD -Path "$env:LOCALAPPDATA\wsl\{distro‑guid}\ext4.vhdx" -Mode Full
Option B – diskpart (works on every Windows edition)
:: Run Command Prompt as Administrator
diskpart
select vdisk file="C:\Users\YourName\AppData\Local\wsl\{distro‑guid}\ext4.vhdx"
compact vdisk
exit
You should see:
DiskPart successfully compacted the virtual disk file.
Verify the new size
Get-Item "$env:LOCALAPPDATA\wsl\{distro‑guid}\ext4.vhdx" |
Select-Object @{N='SizeGB';E={[math]::Round($_.Length/1GB, 2)}}
In practice, compaction can reclaim a few hundred MB up to tens of GB, depending on how long the distro has been used.
6. Enable Sparse Mode (automatic reclamation)
Sparse mode makes WSL automatically shrink the VHDX when you delete files—no manual compaction needed.
# Enable per‑distro
wsl --manage --set-sparse true
# Disable
wsl --manage --set-sparse false
Things to know
- Works per distribution.
- Small performance overhead for the automatic reclamation.
- Most effective on distros that frequently create/delete large files (build artifacts, container images, etc.).
- The distro must be stopped (
wsl --shutdown) when you enable it.
7. Resize the VHDX (if you need more room)
WSL 2 defaults to a 1 TB maximum virtual‑disk size. You can increase the limit (but not decrease it):
wsl --manage --resize 512GB # set new max size
After expanding, extend the filesystem inside the distro:
sudo resize2fs /dev/sdd
8. The WSL UI (one‑click solution)
The WSL Settings UI bundles all the steps above:
| Feature | What it does |
|---|---|
| Compact | Runs fstrim, shuts down WSL, tries Optimize‑VHD first, falls back to diskpart, then shows before/after sizes. |
| Sparse toggle | Enables/disables sparse mode with a single switch. |
| Dashboard | Shows each distro’s disk usage so you can spot growth early. |
If you prefer a GUI, just open Windows Settings → Apps → WSL and use the Compact button.
9. Quick tips to keep WSL disk growth under control
-
Enable sparse mode on development‑heavy distros.
-
Clean package caches regularly:
- Ubuntu/Debian –
sudo apt clean - Fedora –
sudo dnf clean all
- Ubuntu/Debian –
-
Prune container storage (Docker/Podman):
docker system prunepodman system prune
-
Install to a different drive (if C: is small):
wsl --install Ubuntu --location D:\WSL\Ubuntu -
Periodically run the compact steps above (or use the UI) to reclaim space.
TL;DR
sudo fstrim -avinside the distro.wsl --shutdown.- Compact the VHDX (
Optimize‑VHDordiskpart). - Enable sparse mode for automatic future reclamation.
That’s all you need to keep your C: drive from being silently eaten by WSL 2. Happy coding!
Compact before and after major cleanup
If you’re about to delete a lot of data, compact afterward to actually reclaim the space.
Tasks & Commands
| Task | Command |
|---|---|
| Check VHDX size | Get-Item path\ext4.vhdx (PowerShell) |
| Check Linux usage | df -h / (inside the distro) |
| Zero free space | sudo fstrim -av (inside the distro) |
| Shut down WSL | wsl --shutdown |
| Compact (Hyper‑V) | Optimize-VHD -Path "..." -Mode Full |
| Compact (built‑in) | diskpart → select vdisk → compact vdisk |
| Enable sparse mode | wsl --manage --set-sparse true |
| Resize disk | wsl --manage --resize 512GB |
Quick reminder
Your WSL distributions don’t need to consume your entire C: drive. A little maintenance goes a long way.
*Originally published at *