Your WSL2 virtual hard disk file, the vhdx, can grow past 50 GB even if you delete files inside the Linux distribution. The vhdx file does not automatically shrink when you remove data. This article explains why the vhdx file keeps growing and how to reclaim disk space by shrinking it.
Key Takeaways: Shrinking an Overgrown WSL2 vhdx File
- diskpart command
select vdisk file=C:\Users\then\AppData\Local\Packages\...\ext4.vhdx compact vdisk: Shrinks the vhdx file after you zero out free space inside the Linux distribution. - WSL2 command
wsl --shutdown: Shuts down all WSL2 distributions so the vhdx is not locked and can be compacted. - Linux command
sudo fstrim /: Marks all unused blocks as free inside the ext4 file system, which diskpart needs before it can shrink the vhdx.
Why the WSL2 vhdx File Grows Past 50 GB
WSL2 uses a dynamically expanding virtual hard disk in vhdx format. The file starts small and grows as you add data inside the Linux distribution. When you delete files, the ext4 file system marks those blocks as free, but it does not tell the underlying vhdx file to release the space. The vhdx file stays at its largest size until you manually compact it.
The vhdx file is located at C:\Users\. If you run multiple WSL2 distributions, each has its own vhdx file. The default maximum size of a WSL2 vhdx is 256 GB, but it can grow to any size your disk allows. Many users report the vhdx reaching 50 GB, 100 GB, or more even after cleaning up files inside WSL2.
The root cause is that the vhdx format does not automatically reclaim space when the file system inside it frees blocks. You must explicitly run a two-step process: first zero out free space inside the Linux distribution, then compact the vhdx file using the Windows diskpart tool.
Steps to Shrink the WSL2 vhdx File on Windows 11
Follow these steps in order. You need administrator access to run diskpart. The process takes a few minutes depending on the size of your vhdx file.
- Open your WSL2 distribution and run fstrim
Launch your WSL2 terminal. Run the commandsudo fstrim /. This tells the ext4 file system to mark all unused blocks as free. You may need to enter your Linux password. The command completes quickly if you have not run it recently. - Shut down all WSL2 distributions
Open a Windows Command Prompt or PowerShell as administrator. Runwsl --shutdown. This stops all running WSL2 instances and unlocks the vhdx file so diskpart can access it. - Open diskpart
In the same administrator Command Prompt or PowerShell, typediskpartand press Enter. The diskpart tool starts with a new promptDISKPART>. - Select the vhdx file
At the DISKPART prompt, typeselect vdisk file="C:\Users\. Replace the path with the exact location of your WSL2 vhdx file. If you have multiple distributions, repeat this step for each one. Press Enter. diskpart confirms with\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu..._ \LocalState\ext4.vhdx" DiskPart successfully selected the virtual disk file. - Compact the vhdx file
Typecompact vdiskand press Enter. diskpart begins compacting the vhdx file. This can take several minutes for large files. Do not close the window during this process. When finished, diskpart showsDiskPart successfully compacted the virtual disk file. - Exit diskpart
Typeexitand press Enter to close diskpart. You can now restart your WSL2 distribution. Runwslin a normal Command Prompt to start it.
After compacting, check the new size of the vhdx file in File Explorer. The file should be significantly smaller. Repeat this process every few months or whenever you delete large amounts of data inside WSL2.
Common Issues When Shrinking the WSL2 vhdx File
diskpart says the vdisk is not selected or is in use
This error occurs if a WSL2 distribution is still running. Run wsl --shutdown again and verify with wsl --list --running that no distributions are listed. Also close any File Explorer windows that show the vhdx file or its parent folder.
fstrim command returns an error or is not available
Some older WSL2 distributions may not support fstrim. Update your distribution with sudo apt update && sudo apt upgrade on Ubuntu or the equivalent for your distribution. If fstrim still fails, you can run sudo dd if=/dev/zero of=/tmp/zero bs=1M to fill the free space with zeros, then delete the file with sudo rm /tmp/zero. This achieves the same effect as fstrim but takes longer.
The vhdx file does not shrink after compacting
If you did not run fstrim before compacting, the vhdx may not shrink at all. The compact vdisk command only reclaims space that the file system has marked as free. Run fstrim again, then repeat the shutdown and diskpart steps. Also ensure you are compacting the correct vhdx file. Each WSL2 distribution has its own file in a separate folder under LocalState.
WSL2 fails to start after compacting
This is rare. If WSL2 does not start, run wsl --shutdown and then wsl again. If the issue persists, restart Windows. The compact vdisk operation does not modify the file system inside the vhdx, only the container file itself, so data corruption is unlikely.
| Item | Before Compacting | After Compacting |
|---|---|---|
| vhdx file size | 50 GB or larger | Reduced to actual data size |
| Data inside WSL2 | Unchanged | Unchanged |
| Free space inside WSL2 | Not reclaimable by vhdx | Reclaimable by vhdx after fstrim |
| Required commands | None | fstrim, wsl –shutdown, diskpart compact vdisk |
Conclusion
You can now shrink an overgrown WSL2 vhdx file on Windows 11 by running fstrim inside the Linux distribution, shutting down WSL2, and using diskpart to compact the vhdx. This process reclaims disk space without affecting your data. For ongoing maintenance, schedule fstrim to run automatically using a cron job or a systemd timer inside WSL2. Run the compact operation every three months to keep the vhdx file size under control.