How to Shrink Your Massive WSL Virtual Disk

If you use Windows Subsystem for Linux (WSL2), you may notice a large file on your system drive:
C:\Users\<YourUser>\AppData\Local\Packages\<YourDistroFolder>\LocalState\ext4.vhdx
This file is a virtual hard disk where WSL2 stores the entire Linux filesystem. It expands automatically as you install packages and create files, but it does not shrink automatically when files are deleted. Over time, this can leave you with a multi-gigabyte file containing mostly unused space.
This guide shows how to safely reduce the size of the ext4.vhdx
and reclaim disk space in Windows.
Step 1. Free up space inside WSL
Remove unneeded packages and caches so the filesystem has actual free space to reclaim:
sudo apt-get clean
sudo apt-get autoremove -y
Then run TRIM to mark unused blocks as free:
sudo fstrim -v /
Why: Without this step, Windows will not know which blocks are unused, and compaction will have little effect.
Step 2. Shut down WSL completely
From PowerShell or Command Prompt (Administrator):
wsl --shutdown
Check Task Manager to ensure no vmmem
or WSL processes remain.
Why: The virtual disk must not be in use during compaction.
Step 3. Compact the VHDX file
Use diskpart
(built into Windows) to reclaim space:
diskpart
At the DISKPART>
prompt, run the following (adjust the path for your system):
select vdisk file="C:\Users\<YourUser>\AppData\Local\Packages\<YourDistroFolder>\LocalState\ext4.vhdx"
attach vdisk readonly
compact vdisk
detach vdisk
exit
Why: This sequence tells Windows to release unused blocks from the VHDX file.
Step 4. Restart WSL
Re-open your Linux distro:
wsl
Check the filesystem inside Linux:
df -h /
Then verify the reduced size of ext4.vhdx
in Windows Explorer or via file properties.
Why WSL Doesn’t Shrink Automatically
The ext4.vhdx
is a dynamically expanding virtual disk. It grows as data is written but does not contract automatically when files are deleted. This behavior avoids fragmentation and performance issues. Manual compaction, as shown above, is required to reclaim space.
Notes
- The path to
ext4.vhdx
varies depending on your distro. For example, Ubuntu uses aCanonicalGroupLimited.Ubuntu...
folder, while Debian or Fedora use different names. - Always ensure WSL is fully shut down before compacting. Running compaction on an active VHD can corrupt data.
- These steps are safe when followed correctly, but back up important data before performing disk operations.
Comments ()