462

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.

Braiam
  • 1
  • 11
  • 47
  • 78
nikjohn
  • 20,026
  • 14
  • 50
  • 86

5 Answers5

793

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

yarn cache clean
MatthewG
  • 8,583
  • 2
  • 25
  • 27
nikjohn
  • 20,026
  • 14
  • 50
  • 86
  • 10
    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. – daniel.caspers Jun 03 '19 at 15:33
151

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.

Jude Allred
  • 10,977
  • 7
  • 28
  • 27
MienDev
  • 1,739
  • 2
  • 11
  • 6
110

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

inga
  • 3,154
  • 1
  • 25
  • 29
KhaledMohamedP
  • 5,000
  • 3
  • 28
  • 26
  • 10
    its not always in `~/.yarn-cache/` – Pants Jan 03 '20 at 16:10
  • 3
    On my Mac the cache directory is `~/Library/Caches/Yarn/v6` as reported by the `yarn cache dir` command. – pamcevoy Feb 20 '20 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 https://github.com/yarnpkg/yarn/search?q=.yarn-cache&type=Commits – mccallofthewild May 06 '20 at 02:54
  • On my Mac (M1), I found some yarn global modules sitting at `~/.config/yarn/global/node_modules` as well. – Saifur Rahman Mohsin Dec 13 '22 at 06:47
  • ERROR: cache is not COMMAND nor fully qualified CLASSNAME. – Hoang Minh Quang FX15045 Jul 04 '23 at 07: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.

Dan K.K.
  • 5,915
  • 2
  • 28
  • 34
2
  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.

Ali Safari
  • 1,535
  • 10
  • 19