Skip to content
daniel@cosenza:~/blog
WSLHow-To August 25, 2026 2 min read

How to Compact a WSL2 Virtual Disk to Reclaim Disk Space

A complete walkthrough shrinking a WSL2 distro's .vhdx file back down after deleting large amounts of data — the specific diskpart steps that actually reclaim the space Windows doesn't release automatically.

Because WSL2’s virtual disk grows dynamically but doesn’t automatically shrink back down after you delete files inside it, reclaiming that space requires a specific, deliberate compaction process — this walks through doing it correctly.

Step 1: identify which distro’s disk you want to compact

wsl --list --verbose

Step 2: find the exact .vhdx file location

Get-ChildItem -Path "$env:LOCALAPPDATA\Packages" -Recurse -Filter "ext4.vhdx" -ErrorAction SilentlyContinue

This searches for the virtual disk file across installed distro packages — note the specific path for the distro you intend to compact.

Step 3: shut down WSL completely first

wsl --shutdown

The virtual disk cannot be compacted while mounted and in active use — this step is mandatory, not optional.

Step 4: open diskpart as Administrator

diskpart

Step 5: select the virtual disk file

select vdisk file="C:\Users\YourName\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu...\LocalState\ext4.vhdx"

Use the exact path identified in Step 2.

Step 6: attach it in read-only mode

attach vdisk readonly

Attaching read-only is specifically required for the compact operation — this doesn’t modify or risk your data, it’s a prerequisite for the shrink operation itself.

Step 7: run the actual compaction

compact vdisk

This is the step that actually reclaims space — it can take anywhere from seconds to several minutes depending on the virtual disk’s current size.

Step 8: detach and exit diskpart

detach vdisk
exit

Step 9: verify the file size actually decreased

Get-Item "C:\Users\YourName\AppData\Local\Packages\...\LocalState\ext4.vhdx" | Select-Object Length

Compare against the size before compaction to confirm space was genuinely reclaimed.

Step 10: restart your distro normally

wsl -d Ubuntu

Confirm your distro still starts and your files are all intact — compaction only affects the underlying file’s physical size, not its actual logical content.

Step 11: consider scripting this for repeated use

# save the diskpart commands from steps 5-8 as a .txt script
# and run: diskpart /s compact-script.txt

For anyone doing this regularly (after large builds, container image cleanup, or dataset work), scripting the diskpart commands avoids retyping the same steps each time.

Why this manual process is necessary rather than automatic

WSL2’s dynamically-expanding virtual disk format prioritizes fast, simple growth over automatic shrinkage — reclaiming freed space requires this explicit compaction step specifically because that’s how this class of virtual disk technology works generally, not because of anything WSL-specific that could easily be automated away without added complexity elsewhere in the system.