If your Windows Subsystem for Linux (WSL) installation is using more space or behaving strangely, clearing its cache may help. The right steps depend on what you mean by “cache”: downloaded Linux packages, DNS records, application files, or memory used by the WSL virtual machine. There is no single command that safely clears every kind of WSL cache.
This guide explains how to clear common WSL caches, check whether the cleanup worked, and avoid deleting files you still need. The examples primarily use Ubuntu, but most commands apply to other Linux distributions running in WSL. Run Linux commands inside your WSL terminal, and use PowerShell commands in Windows PowerShell or Windows Terminal.
What Does “WSL Cache” Mean?
WSL runs a Linux distribution alongside Windows, so cached data can live in several places. Linux package managers keep downloaded installation files. Applications such as npm and pip maintain their own caches. DNS services may retain name lookups, while WSL 2 uses a virtual machine that can hold memory after Linux processes finish.
These are different kinds of data, and clearing one does not necessarily clear the others. For example, removing Ubuntu’s package download cache can reclaim space inside the Linux filesystem, but it will not clear Windows temporary files or automatically shrink the virtual disk file used by WSL 2.
Before cleaning anything, identify the issue you want to solve. If a package installation is failing, clear the relevant package manager’s cache. If a website hostname is resolving to an old address, flush DNS. If Windows still reports that WSL is using memory, shut down WSL. If the WSL virtual disk remains large, that is a separate disk-compaction task.
Clear the APT Cache in Ubuntu and Debian
On Ubuntu and Debian, APT stores downloaded package files so it can reuse them during installation. Those files can be removed without uninstalling the programs themselves. Open your WSL distribution and run:
sudo apt clean
This removes downloaded package files from APT’s local archive cache. It is generally the simplest first step when you want to clear the Ubuntu WSL cache and free space. If you want to remove package files that are no longer available from configured repositories, use:
sudo apt autoclean
To remove automatically installed dependencies that are no longer needed, run:
sudo apt autoremove
Review the packages APT proposes to remove before confirming. The autoremove command does not simply clear a cache: it removes packages. Do not approve the operation if the list includes software you still use.
You can check how much space package archives occupy with:
sudo du -sh /var/cache/apt/archives
These instructions are for APT-based distributions. Other Linux distributions use different package managers. For example, Fedora commonly uses DNF, while Arch Linux uses Pacman. Check the documentation for your distribution before using a cleanup command, and avoid copying a command for one package manager into another distribution.
Clear Application Caches in WSL
Development tools often use more disk space than the operating system’s package cache. A cache-cleaning command should match the tool that created the files. Clearing one application’s cache generally does not affect other apps, but it may mean downloads or packages need to be fetched again.
-
pip: For Python packages installed with pip, run
python3 -m pip cache purge. If your system uses a virtual environment, activate it first if necessary. The command clears pip’s downloaded wheel and HTTP cache; it does not uninstall installed Python packages. -
npm: To ask npm to verify and clean its cache, run
npm cache clean --force. npm normally manages its cache automatically, so trynpm cache verifyfirst if you are troubleshooting cache corruption rather than reclaiming space. -
Yarn: The command depends on the Yarn version. Yarn Classic commonly supports
yarn cache clean. For modern Yarn, check the version-specific documentation and inspect the cache location before removing files. -
Go: To remove Go’s build cache, run
go clean -cache. This can make future builds slower because Go will rebuild cached results.
Other tools, including language managers, IDEs, and container applications, may store their own data under your Linux home directory or elsewhere. Avoid deleting directories such as ~/.cache wholesale unless you know what they contain. Some applications store useful state or offline data alongside files commonly described as cache.
To look for large directories in your home folder, run:
du -h --max-depth=1 "$HOME" 2>/dev/null | sort -h
This command reports directory sizes; it does not remove anything. Investigate a large directory before deciding whether it is safe to clean.
Flush DNS Cache in WSL
DNS translates hostnames, such as a website address, into IP addresses. If name lookups are stale or failing, flushing the DNS cache may help. The correct method depends on which DNS service your Linux distribution is running.
First, check whether systemd-resolved is active:
systemctl is-active systemd-resolved
If the command reports active, flush its DNS cache with:
sudo resolvectl flush-caches
Some distributions use the older systemd-resolve command instead:
sudo systemd-resolve --flush-caches
If neither service is active, your distribution may not be maintaining a local DNS cache. Restarting a DNS caching service is appropriate only if that service is installed and running; do not assume every WSL distribution uses the same DNS setup.
Windows also maintains a DNS cache. To flush the Windows cache, open PowerShell or Command Prompt and run:
ipconfig /flushdns
This Windows command is separate from the Linux commands. If a hostname still fails, check the network connection, VPN, proxy, DNS settings, and the hostname itself. DNS cache cleanup cannot fix an incorrect address or a server that is offline.
Clear Linux Memory Cache Carefully
Linux uses available memory to cache recently accessed files, which can make later reads faster. This is normal behavior, not usually wasted memory. The kernel can reclaim much of this cache when applications need more RAM, so routinely forcing a memory-cache flush is not recommended.
Linux provides a command that drops certain filesystem caches, but it requires root access and is mainly intended for testing or troubleshooting. It does not clear package downloads, application caches, or files on disk. Avoid using it as a routine “make WSL faster” command; forcing the system to rebuild useful caches can make performance worse.
If WSL 2 appears to be holding memory after you have closed Linux applications, you can stop its virtual machine from Windows. Save your work first, because this shuts down all running WSL distributions. In PowerShell, run:
wsl --shutdown
Start your distribution again from the Start menu, Windows Terminal, or by running wsl. This releases the WSL 2 virtual machine’s current memory allocation back to Windows. It does not delete Linux files, clear every application cache, or remove data from your distribution.
If you only want to stop one distribution, list the running distributions with wsl --list --running, then use wsl --terminate <DistributionName> with the name shown in the list. Terminating a distribution closes its running Linux processes, so save work before doing so.
Check Disk Space Before and After Cleanup
To see available space inside your Linux filesystem, use:
df -h
To inspect the size of a particular directory, use du. For example:
sudo du -sh /var/cache/apt
du -sh ~/.cache
Run these checks before and after cleanup to see whether the specific action changed the amount of space reported by Linux. If you are unsure where space is going, inspect large directories rather than deleting files based on their names alone. System folders such as /var, /usr, and /etc contain important data and should not be removed wholesale.
Why Windows May Not Show the Reclaimed Space
WSL 2 stores its Linux filesystem in a virtual disk. When you delete files inside Linux, the free space becomes available to the Linux filesystem, but the virtual disk file on Windows may not immediately shrink. This can make it look as though clearing the WSL cache did not free space.
Check free space with df -h inside Linux first. If Linux reports more available space but the Windows disk usage remains high, you may be looking at virtual-disk compaction rather than a cache problem. Compaction procedures vary by Windows and WSL version and may require shutting down the distribution. Back up important files and follow current Microsoft documentation before attempting to compact or modify a WSL virtual disk.
Do not delete a WSL virtual disk file manually to recover space. That file contains your distribution’s Linux filesystem, and deleting it can permanently remove your data. If you are considering unregistering a distribution, remember that wsl --unregister deletes that distribution and its files; it is not a cache-cleaning command.
A Safe WSL Cache-Cleaning Routine
For routine maintenance, start with the smallest action that addresses the problem. Update package information if needed, clean the relevant package manager’s cache, and inspect application caches individually. Flush DNS only when you are troubleshooting name-resolution problems. Use wsl --shutdown when you need to stop the WSL 2 virtual machine or release its memory, not as a substitute for deleting disk files.
- Check Linux disk space with
df -hbefore making changes. - Use the cache-cleaning command for the package manager or application that created the data.
- Read removal prompts carefully, especially when running
apt autoremove. - Keep backups of important files before changing distribution or virtual-disk settings.
- Restart WSL only when needed, and save your work before shutting down a distribution.
Conclusion
To clear WSL cache, first identify which cache you mean. Use sudo apt clean for downloaded Ubuntu or Debian package files, the relevant cleanup command for developer tools, and DNS flush commands only for name-resolution issues. If WSL 2 is holding memory, wsl --shutdown stops its virtual machine without deleting your Linux data.
There is no universal WSL cache command, and clearing files inside Linux does not always shrink the Windows virtual disk immediately. Check space before and after cleanup, avoid deleting unknown directories, and use Microsoft’s current guidance for virtual-disk maintenance. These steps help you reclaim the right resources while keeping your WSL distribution safe.
Disclaimer: Articles are written by Humans, AI or Both. Verify Important information.