Your Search Bar For Shrewd Tips

How Do I Clear Wsl Storage?


WSL storage can be confusing: deleting files inside a Linux distribution does not always make the same amount of space available on your Windows drive. That is because WSL 2 stores a distribution’s Linux filesystem in a virtual disk file, which can grow as you add data but may not shrink automatically when you remove it. To clear WSL storage, clean up unnecessary files inside Linux first, then reclaim space from the virtual disk if Windows still shows it as large.

This guide explains how to find what is using space, remove common Linux and Docker clutter, and safely compact a WSL 2 virtual disk. It also covers important precautions so you do not accidentally delete a distribution or its files.

Understand Where WSL Storage Goes

WSL 1 and WSL 2 handle storage differently. WSL 1 uses files in the Windows filesystem, while WSL 2 stores its Linux filesystem in a virtual hard disk, usually a .vhdx file. The instructions for compacting a virtual disk apply to WSL 2.

A WSL 2 virtual disk typically grows as Linux writes data to it. When you delete files inside the distribution, Linux marks their space as available, but Windows may not automatically reduce the size of the VHDX file. As a result, there are two separate tasks:

  • Free space inside Linux: Remove files, caches, packages, logs, or containers you no longer need.
  • Reclaim space in Windows: Shut down WSL and compact the virtual disk so the Windows file can shrink.

Do the Linux cleanup before compacting. Compacting an unchanged disk will not remove files that are still taking up space inside the distribution.

Check Which WSL Distribution Uses Space

Open PowerShell or Windows Terminal and list your installed distributions:

wsl --list --verbose

This shows each distribution’s name and whether it is running under WSL 1 or WSL 2. To check disk usage from inside a Linux distribution, open it and run:

df -h
du -h --max-depth=1 ~ 2>/dev/null | sort -h

df -h shows filesystem usage, while the second command estimates the size of folders in your home directory. To inspect the main Linux filesystem, you can run:

sudo du -xhd1 / 2>/dev/null | sort -h

The scan may take a little time. The -x option keeps the search on the same filesystem, and sudo allows Linux to inspect protected folders. Review large folders before deleting anything; a large directory may contain important projects or application data rather than disposable files.

Remove Package Caches and Unneeded Software

Package managers retain downloaded files, and old packages can accumulate over time. On Ubuntu or Debian, update the package list if needed, clear downloaded package files, and remove unused dependencies:

sudo apt clean
sudo apt autoremove

Read the proposed removal list before confirming. apt clean removes cached package downloads; it does not uninstall installed applications. apt autoremove can remove packages that were installed as dependencies and are no longer required, so check the list carefully.

For Fedora, you can clear DNF’s cached files with:

sudo dnf clean all

For Arch Linux, remove cached package files selectively rather than deleting the entire package cache without checking what you need. For example, if you have the paccache utility installed, this keeps recent package versions:

sudo paccache -r

Commands vary by distribution. Use the package manager that matches your Linux environment, and avoid copying a cleanup command from another distribution without checking what it does.

Clear Logs, Temporary Files, and Language Caches

System logs and temporary files can also grow. On distributions that use systemd, check how much space the journal uses:

journalctl --disk-usage

If you want to limit archived journal logs to a chosen size, use a limit that suits your needs. For example, this keeps the journal at about 200 MB:

sudo journalctl --vacuum-size=200M

Do not remove log files blindly if you are troubleshooting a problem; logs may be useful for diagnosing it. Also check project-specific caches before clearing them. Python, Node.js, Ruby, and other development tools can store downloaded packages or build artifacts, but removing those caches may mean they need to be downloaded again.

To find large files in your home directory, run:

find ~ -type f -size +500M -print 2>/dev/null

Inspect the results and remove only files you recognize and no longer need. Consider moving important archives, virtual environments, build outputs, or datasets to another location rather than deleting them. In particular, avoid manually deleting files from system directories such as /usr, /var, or /etc unless you understand their purpose.

Clean Up Docker Data Stored in WSL

If you use Docker Desktop with the WSL 2 backend, Docker images, containers, and build cache can consume substantial disk space. First review what Docker is using:

docker system df

To remove unused containers, networks, dangling images, and build cache, you can run:

docker system prune

For more aggressive cleanup, Docker offers:

docker system prune -a

Be cautious: the -a option can remove images that are not currently used by a container, even if you planned to use them later. Add --volumes only if you have checked the volumes and are certain you do not need their data. Volumes may contain databases or other persistent information; deleting one can permanently remove that data.

Shut Down WSL Before Reclaiming Windows Space

After cleaning up Linux, close applications that use WSL. Then open PowerShell and shut down all running WSL distributions:

wsl --shutdown

This step is important because the virtual disk must not be in use while you compact it. If you are unsure which distribution is running, check with wsl --list --verbose. You can also stop a single distribution with:

wsl --terminate <DistributionName>

Replace <DistributionName> with the exact name shown in the list. For example, the distribution might be named Ubuntu or Debian.

Find the WSL 2 VHDX File

Each WSL 2 distribution has a virtual disk file, commonly named ext4.vhdx. For distributions installed through the Microsoft Store, it is often located inside a package folder under your Windows user profile, in a path similar to:

%LOCALAPPDATA%\Packages\<DistributionPackage>\LocalState\ext4.vhdx

The package folder name differs by distribution and installation method. Do not assume that every VHDX is in this exact location. You can search your user profile for ext4.vhdx, or use the distribution’s documented storage location. Before working with the file, confirm that it belongs to the distribution you intend to clean.

It is wise to back up important WSL data before changing a virtual disk. You can export a distribution from PowerShell with:

wsl --export <DistributionName> <BackupFile.tar>

Choose a backup location with enough free space. An export may be large, but it provides a way to restore the distribution if something goes wrong.

Compact a WSL 2 Virtual Disk

One option is PowerShell’s Optimize-VHD command. It is available on supported Windows editions when the Hyper-V PowerShell tools are installed. Run PowerShell as an administrator, make sure WSL is shut down, and use the full path to the correct VHDX:

wsl --shutdown
Optimize-VHD -Path "C:\Path\To\ext4.vhdx" -Mode Full

Replace the example path with the real path to your distribution’s disk. If PowerShell says that Optimize-VHD is not recognized, the required Hyper-V module may not be installed or available on your Windows edition. Do not download a random disk utility to work around this; use a supported Windows method instead.

Some current WSL versions also support sparse virtual disks through the WSL management command. Check the options available on your computer with:

wsl --help

If your version lists the sparse-disk option, follow its displayed syntax and documentation for your distribution. WSL commands and features can differ by version, so do not assume an option exists on an older installation.

Compacting may take a while, especially for a large disk. Keep the computer powered on, do not interrupt the operation, and confirm afterward that the VHDX file is smaller. The amount reclaimed depends on how much space was actually freed and whether the disk contains data Windows can compact.

Reclaim Space by Exporting and Reimporting

If you cannot use a supported compacting method, exporting and reimporting a distribution to a new location can be another approach. This is more involved and should be treated as a migration, not a casual cleanup. First export the distribution and verify that the backup file exists. Then, only after confirming the backup, unregister the old distribution and import the export again.

wsl --export <DistributionName> <BackupFile.tar>
wsl --unregister <DistributionName>
wsl --import <NewName> <InstallLocation> <BackupFile.tar> --version 2

Warning: wsl --unregister permanently deletes the named distribution and its local data. Never run it before you have a valid export and have checked that you can access the backup. Importing under a new name may also affect default-user settings, shortcuts, scripts, or applications that expect the original distribution name. If you are not comfortable restoring a backup, use a less destructive cleanup method.

Common WSL Storage Mistakes to Avoid

  • Deleting the VHDX in File Explorer: This removes the distribution’s filesystem and can destroy all its data. Do not delete the virtual disk as a way to “clear” it.
  • Expecting Linux deletion to shrink Windows storage immediately: Linux may have free space while the VHDX file remains large. Compact the disk after cleanup when supported.
  • Removing Docker volumes without checking them: Volumes may hold databases, uploads, or other persistent application data.
  • Running cleanup commands as root without review: Check command output and paths before confirming deletions.
  • Forgetting to stop WSL: Compacting a disk that is in use can fail. Run wsl --shutdown first.

Conclusion

To clear WSL storage, first identify what is using space inside your Linux distribution. Remove package caches, unneeded software, logs, temporary files, and Docker data only after checking what will be deleted. Then shut down WSL and compact the correct WSL 2 VHDX using a supported Windows method. Remember that freeing space inside Linux and reducing the virtual disk’s size on Windows are separate steps. Back up important data before using disk-management commands, and never unregister a distribution unless you have verified a restorable export.


Disclaimer: Articles are written by Humans, AI or Both. Verify Important information.

Shrewdnia

Shrewdnia

Shrewdnia is a destination for curious minds seeking clarity, knowledge, and informed perspectives. Through insightful articles and practical guides our passionate team explores a wide range of topics designed to help readers understand the world around them, make smarter decisions, and stay informed in an ever-changing landscape.


💡 Every question sparks discovery, and every perspective enriches the conversation. Share your thoughts and insights in the comments 👇

Back to blog

Leave a comment

JOIN THE SHREWDNIA COMMUNITY FORUM

What do you think?

Have an opinion, experience, or question about this topic? Join the Shrewdnia Forum and share your thoughts with other readers.

Join the Forum →