Your WSL2 distro says it is using 28 GB, but ext4.vhdx occupies 90 GB on Windows. That gap is normal: Linux freed the blocks, but the virtual disk file did not give them back to Windows.
This guide shows how to measure that gap, choose between the built-in compaction command, sparse VHDs, DiskPart, and Optimize-VHD, and verify that the distro still boots and writes files afterward.
Table of Contents
- Why the ext4 VHDX Keeps Growing
- Measure the Gap First
- Choose a Compaction Method
- Run the Safe Compaction Sequence
- Verify the Distro After Shrinking
- Stop the VHDX From Growing Back
- Conclusion
Why the ext4 VHDX Keeps Growing
Every WSL2 distribution stores its Linux root filesystem inside a virtual hard disk formatted with ext4. On Windows, that disk is the ext4.vhdx file. WSL expands the file as you install packages, pull Docker images, or write project files, up to a default maximum of 1 TB. Microsoft's WSL disk-space guide explains this layout and notes that the Linux side always sees the maximum virtual size, even when the host file is much smaller.
The problem is that the VHDX behaves like a high-water mark. When you delete files inside Linux, ext4 marks those blocks as free, but the host file does not automatically punch holes in itself. The guest filesystem knows which blocks are unused; Windows does not. The fstrim manual describes the missing step: it discards blocks that are not in use by a mounted filesystem, telling the underlying virtual disk which areas can be reclaimed.
Compaction is the host-side operation that then reduces the physical VHDX file. It does not clean files inside Linux, and it does not change the distro's 1 TB virtual ceiling.
Measure the Gap First
Start by comparing what Linux uses with what Windows allocated.
Inside the distro, run:
wsl -d Ubuntu -- df -h /
Replace Ubuntu with your distribution name. The output shows a Size close to 1.0T, a Used value, and an Avail value. The Used column is the number that matters for compaction.
On Windows, inspect the VHDX file itself. This PowerShell snippet finds the largest ext4.vhdx under the default package location:
Get-ChildItem "$env:LOCALAPPDATA\Packages" -Filter ext4.vhdx -Recurse -ErrorAction SilentlyContinue |
Sort-Object Length -Descending |
Select-Object FullName, @{n='SizeGB';e={[math]::Round($_.Length / 1GB, 2)}}
If you imported the distro to a custom folder, use that path instead. The default location is under %LOCALAPPDATA%\Packages\<DistroPackage>\LocalState\ext4.vhdx.
Compare the Linux Used figure with the Windows SizeGB figure. A distro using 30 GB inside Linux but occupying 85 GB on Windows has roughly 55 GB of reclaimable space, minus filesystem metadata. If the two numbers are close, compaction will not help much; clean up inside Linux first.
On a sparse VHDX, the file's logical size can look larger than the space it actually occupies. Check the "Size on disk" value in Explorer rather than relying only on the "Size" column.
Choose a Compaction Method
There are four mechanisms, and they are not interchangeable. Pick the one that matches your WSL version and whether the disk is already sparse.
| Method | Best for | Important constraint |
|---|---|---|
wsl --manage <Distro> --compact |
Current WSL 2.9.8+ or 3.x, one-time cleanup | Distro must be stopped; WSL 2 only |
DiskPart compact vdisk
|
Older WSL builds, Windows Home, manual fallback | Administrator prompt; attach read-only, then detach |
Optimize-VHD -Mode Full |
Windows Pro, Enterprise, or Education with Hyper-V PowerShell | VHDX must be detached or attached read-only |
| Sparse VHD | Repeated regrowth and ongoing reclamation | Opt-in; existing distros need conversion |
The built-in command is the simplest option on a current release. A merged WSL pull request documents wsl --manage <Distro> --compact as a manual VHDX compaction command that uses the Windows Virtual Disk API and rejects a running distro with WSL_E_DISTRO_NOT_STOPPED. The feature first appeared in the WSL 2.9.8 pre-release notes and is carried into the 3.x releases.
If the command is not recognized, update WSL with wsl --update, or fall back to DiskPart. Microsoft's archived DiskPart walkthrough uses select vdisk, attach vdisk readonly, compact vdisk, and detach vdisk in that order. It does not require the Hyper-V role.
Optimize-VHD is another option on editions that include the Hyper-V PowerShell module. Microsoft's Optimize-VHD reference says the virtual disk must be detached or attached in read-only mode, and warns that compaction can succeed without reducing the file size if no optimization is possible.
Sparse VHDs are different: they are designed for ongoing reclamation, not a one-time shrink. Microsoft's September 2023 WSL update introduced the sparseVhd setting for new disks and the --set-sparse command for existing distributions. Sparse support has changed across WSL releases, so check wsl --help on your build before relying on it.
Do not stack methods blindly. If the VHDX is already sparse, Windows compaction tools may reject it because they expect an ordinary dynamic VHDX. Check the file's mode first:
fsutil sparse queryflag "C:\path\to\ext4.vhdx"
If it reports that the file is sparse, treat sparse mode as your current mechanism rather than forcing DiskPart or Optimize-VHD on top of it.
Run the Safe Compaction Sequence
1. Back up the distro
Shut down WSL and export a backup before changing the virtual disk:
wsl --shutdown
New-Item -ItemType Directory -Force D:\wsl-backups
wsl --export Ubuntu D:\wsl-backups\ubuntu-before-compact.tar
Microsoft's WSL command reference documents wsl --export as a snapshot export and wsl --import as the way back in. Replace Ubuntu and the backup path with your own.
2. Trim free blocks inside Linux
Start the distro and run fstrim against the root filesystem:
wsl -d Ubuntu
sudo fstrim -v /
exit
The -v flag prints how much was trimmed. If it reports 0 B, the filesystem may already have discarded its free blocks, or the mount does not support discard; compaction may still reclaim zeroed areas, but do not expect a large drop.
3. Shut down and confirm the stopped state
wsl --shutdown
wsl -l -v
The STATE column for your distro should say Stopped. The built-in command rejects a running distro, and DiskPart and Optimize-VHD need the VHDX detached or read-only. Close Docker Desktop and any editor that may still hold a WSL session.
4. Compact the VHDX
Use the method that matches your system.
Built-in command:
wsl --manage Ubuntu --compact
Wait for the command to return, then check the file size. The command compacts the VHDX; it does not clean files inside Linux or replace the fstrim step.
DiskPart fallback:
Open an Administrator Command Prompt or PowerShell window, then run:
diskpart
select vdisk file="C:\Users\<you>\AppData\Local\Packages\<DistroPackage>\LocalState\ext4.vhdx"
attach vdisk readonly
compact vdisk
detach vdisk
exit
Do not skip detach vdisk. If the VHDX remains attached, WSL may fail to start with an error saying the file is in use.
Optimize-VHD fallback:
From an elevated PowerShell window with the Hyper-V module available:
Optimize-VHD -Path "C:\path\to\ext4.vhdx" -Mode Full
5. Recheck the file size
Get-Item "C:\path\to\ext4.vhdx" |
Select-Object FullName, @{n='SizeGB';e={[math]::Round($_.Length / 1GB, 2)}}
A successful compaction should reduce the physical size, though the exact result depends on how much free space was trimmed and how the VHDX was allocated.
Verify the Distro After Shrinking
A smaller file is not proof that the distro survived. Boot it and test the filesystem:
wsl -d Ubuntu -- echo ok
wsl -d Ubuntu -- bash -lc 'df -h /; touch "$HOME/.compact-check" && rm "$HOME/.compact-check" && echo writable'
The second command should print the filesystem usage, then writable. Check the root mount and kernel messages:
wsl -d Ubuntu
findmnt -n /
sudo dmesg | grep -i "EXT4-fs error" || echo "no EXT4 errors"
sudo apt-get check
exit
If you use Docker, start Docker Desktop and confirm the engine responds:
docker ps
docker images
Also check that critical services start. If the distro uses systemd, systemctl --failed is a quick review.
If WSL drops to a read-only mount after compaction, do not force writes or run e2fsck against a mounted root filesystem. Microsoft's disk-space guide documents a repair path that mounts the VHDX separately and runs e2fsck on the unmounted device.
Stop the VHDX From Growing Back
Compaction is a cleanup, not a permanent fix. The VHDX will grow again as you install packages, pull images, and write files.
Use sparse VHDs for ongoing reclamation
For new distributions, add this to %USERPROFILE%\.wslconfig:
[experimental]
sparseVhd=true
This tells WSL to create new VHDs as sparse files, which Microsoft describes as automatically reducing their size as space is freed.
For an existing distribution, stop WSL and convert it:
wsl --shutdown
wsl --manage Ubuntu --set-sparse true
Some WSL builds gate sparse conversion behind an --allow-unsafe flag or a different experimental setting. If the command is rejected, run wsl --help and follow the syntax shown for your version rather than assuming the flag is available.
Clean the usual space consumers
Before the next compaction, remove data you no longer need:
sudo apt-get clean
sudo journalctl --vacuum-size=200M
docker system prune -a
Review the Docker command before running it: docker system prune -a removes unused images, not just stopped containers. Docker Desktop may also keep its data in a separate VHDX, which needs its own cleanup and compaction sequence.
Trim on a schedule
If your distro runs systemd, check whether the weekly trim timer is active:
systemctl status fstrim.timer
If it is not active, enable it:
sudo systemctl enable --now fstrim.timer
Without systemd, run sudo fstrim -v / periodically, or before each manual compaction.
Know when compaction is the wrong fix
Compaction reduces the host file's physical size, but it does not shrink the distro's virtual maximum or create capacity on a full drive. If the same small C: drive keeps refilling after every cleanup, the durable fix is to give the distro more room. Moving the WSL2 file system to a larger drive puts the VHDX on a disk that can accommodate its growth, rather than repeating the shrink cycle every few weeks.
Conclusion
The gap between df and the ext4.vhdx size is expected: Linux frees blocks, but Windows keeps the high-water mark until you trim and compact. Measure the gap first, back up the distro, run fstrim, stop WSL completely, and then compact with the built-in command, DiskPart, or Optimize-VHD.
Use the built-in wsl --manage <Distro> --compact on a current WSL release, fall back to DiskPart on older builds, and choose sparse VHDs when the file keeps regrowing. Always boot the distro afterward and test that the root filesystem is writable. A successful shrink should leave you with a smaller VHDX and a working Linux environment, not just a smaller number in Explorer.
Top comments (0)