If you use Windows Subsystem for Linux (WSL2), you may have noticed a large file on your system drive:
C:\Users\<YourUser>\AppData\Local\Packages\<YourDistroFolder>\LocalState\ext4.vhdx
This is a virtual hard disk that 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.
Here’s how to safely compact it and reclaim that space.
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 /
Without this step, Windows won’t know which blocks are unused and compaction will have little effect.
Step 2: Shut Down WSL Completely
From PowerShell or Command Prompt (as Administrator):
wsl --shutdown
Check Task Manager to confirm no vmmem or WSL processes remain. The virtual disk must not be in use during compaction.
Step 3: Compact the VHDX File
Use diskpart (built into Windows) to compact the file:
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
This sequence tells Windows to release unused blocks from the VHDX file back to the host filesystem.
Step 4: Restart WSL and Verify
Re-open your Linux distro:
wsl
Check the filesystem inside Linux:
df -h /
Then check the size of ext4.vhdx in Windows Explorer (right-click → Properties) and compare it to before.
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 when files are deleted — this avoids fragmentation and performance issues. Manual compaction is the only way to reclaim the space.
Notes
- The path to
ext4.vhdxvaries by distro. Ubuntu uses aCanonicalGroupLimited.Ubuntu...folder; Debian and Fedora use different names. - Always ensure WSL is fully shut down before compacting. Running compaction on an active VHD can corrupt data.
- Back up any important data before performing disk operations.