Yarn, a popular package manager for JavaScript projects, has become increasingly popular among developers due to its speed, reliability, and security. One of the factors contributing to Yarn’s speed is its caching system, which stores previously downloaded packages to make future installations faster. However, there are times when clearing the cache becomes necessary, such as when a package version has been updated or when you need to reclaim disk space. In this article, we will explore how to clear the cache in Yarn using the yarn cache clean command and discuss some best practices to optimize your Yarn cache.
Table of Contents:
- Understanding Yarn Cache
- When to Clear Yarn Cache
- Clearing Yarn Cache: The Basics
- Clearing Cache for Specific Packages
- Best Practices for Managing Yarn Cache
- Troubleshooting Common Issues
- Conclusion
1. Understanding Yarn Cache
Yarn cache is a local storage system that stores previously downloaded packages to speed up future installations. The cache is usually stored in a global folder, allowing multiple projects to share the same cached packages. By default, Yarn cache is stored in the following directories:
- On macOS and Linux: ~/.cache/yarn
- On Windows: %LOCALAPPDATA%\Yarn\Cache
2. When to Clear Yarn Cache
Some common reasons to clear the Yarn cache include:
- Updating a package to a new version
- Removing corrupt or incomplete package files
- Reclaiming disk space
- Troubleshooting installation issues
3. Clearing Yarn Cache: The Basics
To clear the entire Yarn cache, run the following command in your terminal or command prompt:
yarn cache clean

This command will remove all cached packages, allowing you to start with a fresh cache.
4. Clearing Cache for Specific Packages
To remove the cache for a specific package, you can use the following command:
1 | yarn cache clean <package-name> |
Replace <package-name> with the name of the package you want to clear the cache for. For example:
yarn cache clean lodash
This command will remove all cached versions of the specified package.
5. Best Practices for Managing Yarn Cache
- Regularly check for updates to packages and clear the cache for those packages as needed
- Keep track of your cache size and clear it periodically to save disk space
- Use Yarn’s offline mirror feature to store tarballs of your dependencies locally, making installations even faster
6. Troubleshooting Common Issues
- If you encounter issues after clearing the cache, try reinstalling your packages with `yarn install`
- If you suspect your cache is corrupted or causing issues, you can verify its integrity with `yarn cache verify`
Conclusion
In this article, we have learned how to clear the cache in Yarn using the yarn cache clean command. By maintaining a clean and efficient cache, you can ensure your Yarn projects remain fast and reliable. Make sure to follow best practices and clear the cache when necessary to optimize your Yarn cache management.