Clearing Pip Cache
You can built-in cache functionality in pip that plays its role by reducing the time while doing duplicate downloads and builds.
But you may find a situation where the package won't be updated to the latest version as the cache for the older version exists on your system.
And in this tutorial, I will walk you through how you can clear the pip cache.
How to clear Pip cache
First, let's have a look at how you can find the cache size of pip.
To do so, all you have to do is execute the following command:
pip cache info
And as you can see, the package index cache is 101
MB in my system whereas 17.1
MB of the locally built cache by wheels.
Similarly, if you want to find out the cache of the individual packages, you can use the following:
pip cache list
And if you want to find the directory where the cache resides, you can use the following command:
pip cache dir
Now, let's have a look at how you can remove the pip cache.
Remove Pip cache
Once you know which package is taking the most space with the unnecessary cache, you can refer to the given ways to remove the pip cache:
- Using the
pip remove
(used to remove individual packages from cache) - Using the
pip purge
(used to remove every package from the cache)
So let's start with how you can remove individual packages from the pip cache.
Removing individual packages from the Pip cache
To remove the individual packages from the cache, you will have to execute the pip remove
command in the following manner:
pip remove [pattern]
Here, if you want to remove a specific package from the cache, you can append the package name instead of [pattern]
as shown:
pip cache remove [package_name]
So let's say I want to remove the cache of the package named moviepy
, then I will be using the following:
pip cache remove moviepy
Similarly, if you want to remove every package from the cache using the pip cache remove
using the *
wildcard:
pip cache remove *
Remove every package from the Pip cache
You may encounter such a situation where you have to remove everything from the pip cache and for those times, you have pip cache purge
.
To use the purge with the pip cache, all you have to do is execute the following command:
pip cache purge
And now if you check the cache list, it will show you something like this:
Remove the pip cache manually
If you don't have access to the pip cache command, you can also remove the pip cache directory manually.
Here, I will be giving the solution for both Linux and Windows so execute the command accordingly.
For Linux:
sudo rm -r ~/.cache/pip
sudo rm -rf /root/.cache/pip
For Windows:
rd /s /q "%appdata%\local\pip\cache"
Bonus: Install the pip packages without cache
So if you want to install the pip packages without cache, you will have to use the --no-cache-dir
flag while installing the pip package:
pip install package_name --no-cache-dir
So let's say I want to install django-cricket-statistics
without cache then, I will be using the following:
pip install django-cricket-statistics --no-cache-dir
Pretty cool. Isn't it?
Want to learn Python for free? Let me help
If you are just getting started and looking for some free resources to learn python, we have shortlisted some of the best courses and e-books for you:
I hope you will find this guide helpful.
And if you have any issues, feel free to ask in the comments.