Linux & Servers

How to Shrink WSL 2 Disk Space & Compact ext4.vhdx

If you use the Windows Subsystem for Linux (WSL 2) for development, coding, or running Linux commands on Windows 10 or Windows 11, you might have noticed a frustrating issue: your C drive is slowly running out of storage.

Even if you delete large files, packages, or Docker containers inside Linux, WSL's virtual hard drive (ext4.vhdx) tends to hold onto that space permanently. Windows sees the file as large and won't reclaim it automatically, leading to severe storage bloat.

Fortunately, you can easily reclaim that wasted disk space. Here is a quick, beginner-friendly guide to shrinking your WSL 2 virtual disk safely without losing your data.

Step 1: Backup

Create backup of the existing WSL distro by executing following command from Powershell.

wsl -l -v
wsl --export <distro> backup.tar

Step 2: Trim Unused Blocks Inside Linux

Before Windows can shrink your virtual disk, you need to tell Linux which blocks of data are no longer in use so the system knows what can actually be recovered.

  1. Open your WSL distribution terminal (such as Ubuntu).
  2. Run the following command and enter your Linux password when prompted:
sudo fstrim -av
  1. Once the trim process finishes successfully, close your WSL terminal window.

Step 3: Shut Down the WSL service

Windows cannot resize or modify a virtual hard drive while it is actively running. You must shut down all WSL instances completely from PowerShell:

wsl --shutdown

Step 4: Find Your Virtual Disk File (ext4.vhdx)

Next, you need to locate where your specific WSL distribution's virtual disk file is stored on your PC. Copy and paste this command into PowerShell:

Get-ChildItem HKCU:\Software\Microsoft\Windows\CurrentVersion\Lxss | ForEach-Object { Get-ItemProperty $_.PSPath } | Select DistributionName, BasePath

Copy the FullName path from the output. It will look similar to this:
C:\Users\YourUsername\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu_...\LocalState\ext4.vhdx

Step 5: Compact the Disk using Diskpart

Now that you have your file path, you can use Windows' built-in disk management utility to shrink the virtual hard disk down to its actual size.

  1. Open the Start menu, type PowerShell or Command Prompt, right-click it, and select Run as administrator.
  2. Type diskpart and press Enter to open the disk partitioning tool.
  3. Run the following commands one by one. Make sure to replace the placeholder path with the exact FullName path you copied in Step 3:
select vdisk file="C:\Users\YourUsername\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu_...\LocalState\ext4.vhdx"
attach vdisk readonly
compact vdisk
detach vdisk
exit

Troubleshooting

  • Access denied in Powershell - The Powershell was not opened with Administrator privileges.
  • compact finished but nothing shrank - The Windows is still thinking that deleted data in the WSL2 sector is still occupied. Execute sudo fstrim -v /

Summary and Wrap-Up

That is all it takes! Open up Windows File Explorer and check your storage drive properties—you should instantly see a noticeable amount of newly freed disk space.

To keep your system running smoothly, you can bookmark this guide and repeat these steps every few months whenever your WSL 2 environment starts feeling a bit bloated again.

Show More

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back to top button