Managing WSL Disk Space

Managing WSL Disk Space architecture diagram
Click to expand
1527 × 260px

If you've been using WSL2 for a while, you've probably noticed your C: drive slowly losing space. Maybe 10GB here, 50GB there. The culprit? WSL2's virtual hard disk files — they grow as you use them but never shrink back on their own.

This is one of those things that catches people off guard. You delete 20GB of files inside your distro, check your Windows disk space, and... nothing changed. The space is still gone.

Let's fix that.

How WSL2 Stores Data

WSL2 runs each distribution inside a lightweight virtual machine. Your Linux filesystem lives in a VHDX file — a Hyper-V virtual hard disk. When you install packages, download files, or build projects inside WSL, this VHDX file grows to accommodate the data.

The problem: when you delete those files, the VHDX doesn't shrink. The file on your Windows drive stays the same size, even though the Linux filesystem inside it now has free space. Over months of use, this gap between actual usage and file size can become substantial.

Finding Your VHDX Files

First, let's see what we're dealing with. Your VHDX files are typically stored in one of these locations:

  • Default (newer WSL): %LOCALAPPDATA%\wsl\
  • Store-installed distros: %LOCALAPPDATA%\Packages\<distro-package>\LocalState\
  • Custom locations: Wherever you specified during wsl --import

To find the exact path for a specific distro, check the Windows Registry:

HKCU\Software\Microsoft\Windows\CurrentVersion\Lxss\{GUID}\BasePath

The VHDX file is always called ext4.vhdx inside that base path.

Checking the Damage

From PowerShell, you can see all your VHDX sizes at once:

powershell
Get-ChildItem -Path "$env:LOCALAPPDATA\wsl" -Recurse -Filter "ext4.vhdx" | Select-Object FullName, @{N='SizeGB';E={[math]::Round($_.Length/1GB, 2)}}

Inside each distro, check actual disk usage with:

bash
df -h /

Compare those numbers. If your VHDX is 45GB but df shows only 20GB used, you have 25GB of reclaimable space.

Reclaiming Disk Space

Compacting a VHDX is a multi-step process. You need to zero out the free space inside the Linux filesystem, shut down WSL so the file isn't locked, then compact the VHDX from Windows.

managing-wsl-disk-space/compact-workflow diagram
Click to expand
344 × 733px

Step 1: Zero Unused Blocks with fstrim

The fstrim command tells the filesystem to discard blocks that are no longer in use. This zeros them out so the VHDX compactor can identify and reclaim the space.

Run this inside your distro (as root):

bash
sudo fstrim -av

You should see output like:

/: 12.5 GiB (13421772800 bytes) trimmed on /dev/sdd

That number tells you how much space is potentially reclaimable.

Alpine Linux note: Alpine uses BusyBox which has a simpler fstrim. Use sudo fstrim -v / instead of -av.

Step 2: Shut Down WSL

The VHDX file must not be in use when you compact it. From PowerShell or Command Prompt:

powershell
wsl --shutdown

This stops all running distributions and the WSL2 VM. Wait a few seconds for the filesystem to fully release.

Step 3: Compact the VHDX

You have two options here, depending on your Windows setup.

Option A: Optimize-VHD (Hyper-V required)

If you have Hyper-V enabled (Windows Pro/Enterprise), this is the faster and more reliable method. Run PowerShell as Administrator:

powershell
Optimize-VHD -Path "$env:LOCALAPPDATA\wsl\{distro-guid}\ext4.vhdx" -Mode Full

Option B: diskpart (works everywhere)

If you don't have Hyper-V, diskpart is built into every Windows installation. Run Command Prompt as Administrator:

diskpart select vdisk file="C:\Users\YourName\AppData\Local\wsl\{distro-guid}\ext4.vhdx" compact vdisk exit

Wait for the "DiskPart successfully compacted the virtual disk file" message.

Step 4: Verify

Start your distro again and check the Windows file size:

powershell
wsl -d Ubuntu -- echo "Distro started" Get-Item "$env:LOCALAPPDATA\wsl\{distro-guid}\ext4.vhdx" | Select-Object @{N='SizeGB';E={[math]::Round($_.Length/1GB, 2)}}

You should see a smaller file size. In practice, I've seen compaction reclaim anywhere from a few hundred MB to tens of GB depending on how long the distro has been running.

Enabling Sparse Mode (Automatic Reclamation)

Manually compacting is fine occasionally, but WSL has a better long-term solution: sparse mode. When enabled, WSL automatically reclaims disk space as you delete files — no manual compaction needed.

Enable it per-distro:

powershell
wsl --manage <distro-name> --set-sparse true

For example:

powershell
wsl --manage Ubuntu --set-sparse true

To disable it:

powershell
wsl --manage <distro-name> --set-sparse false

Things to know about sparse mode:

  • It works on a per-distribution basis
  • There's a small performance overhead for the automatic reclamation
  • It's most effective on distros where you frequently create and delete large files (build artifacts, container images, etc.)
  • Your distro needs to be stopped when you enable it

Resizing the Virtual Disk

Sometimes you need the opposite — more space. WSL2 defaults to a 1TB maximum virtual disk size, but you can expand it:

powershell
wsl --manage <distro-name> --resize 512GB

This increases the maximum size the VHDX can grow to. You can only increase the size, not decrease it. After resizing, you may need to extend the filesystem inside the distro:

bash
sudo resize2fs /dev/sdd

The Easy Way: WSL UI

All of these steps — finding VHDX files, running fstrim, shutting down WSL, choosing between Optimize-VHD and diskpart — are exactly the kind of multi-step process that's easy to get wrong.

WSL UI handles all of this with a single click. The compact feature:

  1. Runs fstrim inside the distro automatically
  2. Shuts down WSL and waits for the file lock to release
  3. Tries Optimize-VHD first, falls back to diskpart if Hyper-V isn't available
  4. Shows you the before/after sizes and how much space was saved

It also shows disk usage in the main dashboard, so you can spot distros that need attention before your C: drive fills up.

Sparse mode is a toggle in the distribution settings — no command line needed.

Prevention Tips

A few things you can do to keep disk growth under control:

  • Enable sparse mode on distros where you do heavy development
  • Clean package caches regularly: sudo apt clean (Ubuntu/Debian) or sudo dnf clean all (Fedora)
  • Watch Docker/Podman storage — container images inside WSL are a common space hog. Run docker system prune or podman system prune periodically
  • Use --location when installingwsl --install Ubuntu --location D:\WSL\Ubuntu puts the VHDX on a different drive from the start
  • Compact before and after major cleanup — if you're about to delete a lot of data, compact afterward to actually reclaim the space

Summary

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

Your WSL distributions don't need to eat your entire C: drive. A bit of maintenance goes a long way.

← Back to all posts