If your Windows drive is filling up, deleting files inside WSL may seem like an easy fix—but WSL 2 stores each Linux distribution in a virtual disk file that can keep its allocated size even after you remove data. To clear WSL space effectively, clean up files inside Linux first, then compact or configure the virtual disk so Windows can reclaim unused space. The steps below explain how to find what is using storage, clean it safely, and reduce the size of your WSL disk.
Why WSL Space Does Not Always Decrease
WSL 2 stores a Linux distribution’s filesystem in a virtual hard disk, usually an ext4.vhdx file on your Windows drive. When you install software or create files, that virtual disk grows. When you delete files in Linux, the filesystem marks their blocks as available, but the VHDX file may not immediately shrink. As a result, Linux can report plenty of free space while Windows still shows the disk file occupying a lot of storage.
Think of clearing WSL space as two separate tasks: remove data you no longer need from Linux, then reclaim the released space in the virtual disk. Compacting the disk before deleting files will not help, and deleting the VHDX file itself will erase the distribution. Always use the cleanup and compaction steps below rather than manually removing WSL system files.
Check Which WSL Distribution Is Using Space
Start by listing your installed distributions in PowerShell or Windows Terminal:
wsl --list --verbose
This shows each distribution’s name and whether it is running. To check free space inside a distribution, open it and run:
df -h
To identify large folders, try:
sudo du -h --max-depth=1 /home 2>/dev/null | sort -h
Change /home to another location, such as /var, if you suspect that is where the storage is going. For a more interactive view, install and run ncdu if it is available for your Linux distribution. These checks help you find large files and folders before deleting anything important.
Clean Package Manager Files and Unused Software
Package managers retain downloaded files and may leave unused dependencies behind. On Ubuntu or Debian, clear the APT download cache and remove packages that are no longer needed:
sudo apt clean
sudo apt autoremove
apt clean removes cached installation files; it does not uninstall your applications. Review the list shown by apt autoremove before confirming, especially if you have installed packages that are important to your workflow. Other distributions use different package managers. For example, Fedora uses DNF, so check its documentation and commands rather than running APT instructions there.
You can also remove software you no longer use through your distribution’s package manager. Avoid deleting folders under /usr or /var/lib/dpkg manually: doing so can break package management or installed applications.
Remove Caches, Logs, and Temporary Files Carefully
Development tools, browsers, language package managers, and build systems can create sizable caches. Check the folders first, then use each tool’s own cleanup command where possible. For example, Python projects may have virtual environments or build directories, while Node.js projects can contain large node_modules folders that can often be recreated from a lockfile.
To review system journal usage on distributions that use systemd, run:
sudo journalctl --disk-usage
If the journal is larger than you want, you can remove older entries by age or set a size limit. For example, this keeps only the most recent two weeks of journal data:
sudo journalctl --vacuum-time=14d
Use cleanup commands only when you understand what they remove. Do not delete arbitrary files from /tmp, application directories, or system folders simply because their names look temporary. If you are unsure about a large item, search for its purpose before removing it.
Clean Up Docker Data Stored in WSL
Docker images, stopped containers, build caches, and volumes can consume substantial space. If Docker is part of your WSL setup, inspect its storage before pruning it. You can remove unused Docker objects with:
docker system prune
Docker will show what it plans to remove and ask for confirmation. Avoid adding --volumes unless you have checked your volumes: they may contain databases or other data that is not stored elsewhere. Build caches can also be cleared separately, but consider whether you will need to rebuild images and how long that will take.
Docker Desktop may store its data in a separate virtual disk, so cleaning a Linux distribution does not always reduce Docker’s Windows storage. Check Docker Desktop’s storage settings and the location where its data is kept before assuming that a particular WSL distribution contains all Docker files.
Shut Down WSL Before Compacting Its Disk
Once you have deleted unwanted Linux files, stop WSL before working with the VHDX. In PowerShell or Windows Terminal, run:
wsl --shutdown
This stops all running WSL distributions and helps ensure that the virtual disk is not in use. Close applications that may automatically restart WSL, such as terminals, editors, or Docker Desktop, if the disk remains locked. Do not try to compact an active distribution.
Find the WSL VHDX File
A Microsoft Store distribution commonly stores its VHDX inside a package folder under your Windows user profile, often beneath %LOCALAPPDATA%\Packages and a LocalState folder. Imported distributions may use a different location chosen during import. The file is typically named ext4.vhdx.
Use File Explorer or PowerShell to locate the correct file. If you have several distributions, check the package or import location carefully so you do not compact the wrong disk. Never rename, move, or delete a VHDX while WSL is running. Before making changes to the disk, consider exporting your distribution as a backup with wsl --export and storing that backup somewhere with enough free space.
Compact a WSL VHDX with DiskPart
Windows DiskPart can compact a dynamic virtual disk after its unused blocks have been released. Open PowerShell or Command Prompt as an administrator, start DiskPart, and select the correct VHDX. Replace the sample path with the full path to your file:
diskpart
At the DiskPart prompt, run:
select vdisk file="C:\path\to\ext4.vhdx"
compact vdisk
exit
Allow the operation to finish before restarting the distribution. Compacting may take time, and the amount of space reclaimed depends on how much data was removed and whether the free blocks can be recognized by the virtual disk. If DiskPart reports that the disk is in use, run wsl --shutdown again and close programs that might be keeping it open.
DiskPart changes the virtual disk file, so confirm the path and back up important data first. Do not use DiskPart commands to detach, delete, or modify a disk unless you know exactly what those commands do.
Use Optimize-VHD If It Is Available
Some Windows editions and installations have the Hyper-V PowerShell module, which provides the Optimize-VHD command. If it is available, shut down WSL and run PowerShell as an administrator:
Optimize-VHD -Path "C:\path\to\ext4.vhdx" -Mode Full
Replace the example path with the actual VHDX location. This option is not available on every Windows setup; if PowerShell says the command is not recognized, use DiskPart instead. Do not try to install or enable Windows virtualization features without checking that they are appropriate for your edition and system configuration.
Consider Sparse VHD Support for Future Cleanup
Recent versions of WSL provide a sparse virtual disk option for supported distributions. Sparse disks can make it easier for the virtual disk file to release unused space over time, but behavior depends on your WSL version and configuration. Check that WSL is up to date before using the option:
wsl --update
Then consult the current Microsoft WSL documentation for the exact sparse-disk support and command syntax available on your system. Do not assume enabling sparse mode will automatically remove files or recover every previously allocated block. You still need to clean up data inside Linux, and a one-time compaction may be needed to reclaim existing space.
Common Mistakes to Avoid
- Deleting the VHDX file: This can permanently remove the Linux distribution and its data.
-
Running
wsl --unregisteras a cleanup shortcut: This deletes the named distribution and its filesystem. Use it only when you intentionally want to remove the entire distribution and have backed up anything you need. - Expecting Linux file deletion to shrink Windows storage immediately: Cleanup and VHDX compaction are separate steps.
- Compacting while WSL is running: Shut down WSL first so the virtual disk is not active.
- Pruning Docker volumes blindly: Volumes may contain important application data, including databases.
- Assuming every large file belongs to WSL: Docker Desktop, Windows temporary files, and other applications can use separate storage.
How to Keep WSL Space Under Control
Check disk usage occasionally rather than waiting for Windows to run low on space. Remove old project builds, unused virtual environments, duplicate downloads, and packages you no longer need. Keep backups of important code and data outside the distribution, and review Docker images and caches periodically if you use containers.
If a distribution’s VHDX is still too large after cleanup and compaction, verify that you compacted the right file and that WSL was fully shut down. For a major move or rebuild, exporting the distribution and importing it to a new location can be useful, but it requires enough temporary space for the export and a careful backup. Follow Microsoft’s current instructions, and do not unregister the original distribution until you have confirmed that the new copy works and your data is intact.
Conclusion
To clear WSL space, first identify and remove unnecessary files inside Linux, then shut down WSL and compact the correct ext4.vhdx file with DiskPart or Optimize-VHD when available. Sparse disk support may help with future space management on compatible setups. Back up important data, review destructive cleanup prompts, and never delete a VHDX or unregister a distribution unless you intend to remove its contents.
Disclaimer: Articles are written by Humans, AI or Both. Verify Important information.