How to Clear the Cache in Yarn
Yarn is a powerful and popular tool for managing the dependencies of your Node project.
It is an alternative to
npm, which is the default package manager for Node.
Every time you install a new package using
yarn add, Yarn will store a copy in its cache directory.
This cache is used to speed up your next
yarn add command, however sometimes it can be useful to clear the cache to regain back some storage space.
In this post, we'll learn how to clear the Yarn cache.
Clearing the Yarn Cache
As mentioned before, the cache is created by Yarn as you install the packages for your project.
The default cache directory is
~/.yarn-cache, however, rather than directly modifying this folder, it is safer to use the built-in Yarn commands specifically for clearing the cache.
To clear the cache, run the following command:
yarn cache clean
This command will clear the entire Yarn cache for you, wiping out all packages.
You can, however, clear the cache selectively by specifying the packages you want to remove.
You can do this by specifying the packages you want to remove, separated by a space.
For example, here's how to clear the cache for React:
yarn cache clean react
You can also list out the cache for all the packages currently cached by running this command:
yarn cache list
From there, you can choose to selectively remove packages, or you can remove all packages at once.
In this post, we've learned how to clear the Yarn cache, both in a simple way and selectively.
We even learned how to list out the cache for all the packages currently cached by Yarn.
Hopefully, this helps you get a better handle on your Yarn cache. Thanks for reading!
If you want to learn about web development, founding a start-up, bootstrapping a SaaS, and more, follow me on Twitter! You can also join the conversation over at our official Discord!
Leave us a message!