501

I am doing some benchmark tests for Facebook's Yarn. For this, I need to clear my global Yarn cache.

Is there a command available for this? I have force-removed my ~/.yarn-cache folder, but this seems to be quite manual.

5 Answers 5

861

Ok I found out the answer myself. Much like npm cache clean, Yarn also has its own

yarn cache clean
1
  • 12
    In case others miss this, be certain to run this from the user account running yarn. In my case, I was trying to clean a continuous integration server with a different Windows account than the one our project builds on, and it wasn't removing files until I realized I needed to run it from that account. Commented Jun 3, 2019 at 15:33
160

Run yarn cache clean.


Run yarn help cache in your bash, and you will see:

Usage: yarn cache [ls|clean] [flags]

Options: -h, --help output usage information -V, --version output the version number --offline
--prefer-offline
--strict-semver
--json
--global-folder [path]
--modules-folder [path] rather than installing modules into the node_modules folder relative to the cwd, output them here
--packages-root [path] rather than storing modules into a global packages root, store them here
--mutex [type][:specifier] use a mutex to ensure only one yarn instance is executing

Visit http://yarnpkg.com/en/docs/cli/cache for documentation about this command.

5
  • 34
    That is the same answer from @nikjohn Commented Nov 21, 2016 at 5:58
  • 8
    @LeoCaseiro this is what could be called "race conditions" XD Commented Dec 19, 2020 at 11:41
  • how to clean single package? Commented Jul 12, 2021 at 20:33
  • 2
    @EliavLouski yarn cache clean package-name see documentation for details: classic.yarnpkg.com/en/docs/cli/cache#toc-yarn-cache-clean (it's missing in the documentation of Yarn 2 though, but hopefully, it's still supported).
    – pmrotule
    Commented Oct 29, 2021 at 14:26
  • @MarcoOttina This answer is an entire day later though..
    – James
    Commented Feb 17 at 20:40
113

Also note that the cached directory is located in ~/.yarn-cache/:

yarn cache clean: cleans that directory

yarn cache list: shows the list of cached dependencies

yarn cache dir: prints out the path of your cached directory

8
  • 12
    its not always in ~/.yarn-cache/
    – Pants
    Commented Jan 3, 2020 at 16:10
  • 3
    On my Mac the cache directory is ~/Library/Caches/Yarn/v6 as reported by the yarn cache dir command.
    – pamcevoy
    Commented Feb 20, 2020 at 16:26
  • I have both. yarn cache dir returns ~/Library/Caches/Yarn/v6. But ~/.yarn-cache/ is 2.8GB and hasn't been updated since January 2018. yarn cache clean does not work on ~/.yarn-cache/. It has to be manually deleted. Cache location seems to be a tumultuous subject github.com/yarnpkg/yarn/search?q=.yarn-cache&type=Commits Commented May 6, 2020 at 2:54
  • On my Mac (M1), I found some yarn global modules sitting at ~/.config/yarn/global/node_modules as well. Commented Dec 13, 2022 at 6:47
  • ERROR: cache is not COMMAND nor fully qualified CLASSNAME. Commented Jul 4, 2023 at 7:45
43

In addition to the answer, $ yarn cache clean removes all libraries from cache. If you want to remove a specific lib's cache run $ yarn cache dir to get the right yarn cache directory path for your OS, then $ cd to that directory and remove the folder with the name + version of the lib you want to cleanup.

1
  • ERROR: cache is not COMMAND nor fully qualified CLASSNAME. Commented Jul 4, 2023 at 7:45
3
  1. To clear the cache, run the following command:

    yarn cache clean → This command will clear the entire Yarn cache for you

  2. To clear the cache selectively:

    yarn cache clean packagename → for example: yarn cache clean react

To list out the cache for all the packages currently cached, run the command below:

yarn cache list → From here, you can choose to selectively remove packages.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.