Managing WSL Disk Space

Published: (February 18, 2026 at 06:12 PM EST)
5 min read
Source: Dev.to

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

LocationTypical path
Default (newer WSL)%LOCALAPPDATA%\wsl\
Store‑installed distros%LOCALAPPDATA%\Packages\\LocalState\
Custom locationsWhatever 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:

FeatureWhat it does
CompactRuns fstrim, shuts down WSL, tries Optimize‑VHD first, falls back to diskpart, then shows before/after sizes.
Sparse toggleEnables/disables sparse mode with a single switch.
DashboardShows 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

  1. Enable sparse mode on development‑heavy distros.

  2. Clean package caches regularly:

    • Ubuntu/Debian – sudo apt clean
    • Fedora – sudo dnf clean all
  3. Prune container storage (Docker/Podman):

    • docker system prune
    • podman system prune
  4. Install to a different drive (if C: is small):

    wsl --install Ubuntu --location D:\WSL\Ubuntu
  5. Periodically run the compact steps above (or use the UI) to reclaim space.


TL;DR

  1. sudo fstrim -av inside the distro.
  2. wsl --shutdown.
  3. Compact the VHDX (Optimize‑VHD or diskpart).
  4. 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

TaskCommand
Check VHDX sizeGet-Item path\ext4.vhdx (PowerShell)
Check Linux usagedf -h / (inside the distro)
Zero free spacesudo fstrim -av (inside the distro)
Shut down WSLwsl --shutdown
Compact (Hyper‑V)Optimize-VHD -Path "..." -Mode Full
Compact (built‑in)diskpartselect vdiskcompact vdisk
Enable sparse modewsl --manage --set-sparse true
Resize diskwsl --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 *

0 views
Back to Blog

Related posts

Read more »

Common problems in Windows

Computer is running slowly - Reboot the computer fixes many issues. - If reboot doesn't help, verify sufficient CPU, disk space, and RAM for the OS, hardware,...

How GNOME drives me Crazy on Ubuntu

Problem If you’re using Ubuntu with the default GNOME desktop, you may notice that clicking a pinned app in the dock e.g., Firefox shows thumbnails of all open...