What is the Yarn cache folder, and is it safe to delete?

The Yarn cache folder holds package archives that Yarn downloads once and reuses, and it is safe to clear because yarn install fetches them again.

Shiva ChandraMaker of Clean House · · 2 min read
Short answer

~/Library/Caches/Yarn is the global cache where Yarn Classic keeps downloaded package archives so repeat installs are fast. It is safe to clear. Run yarn cache clean and the cache refills the next time you run yarn install. Your projects and node_modules folders are not touched.

Path
~/Library/Caches/Yarn
What it is
Yarn Classic's global cache of downloaded package archives
Typical size
Several hundred megabytes to many gigabytes, depending on how many projects you install
Made by
Yarn (Classic 1.x)
Safe to delete?
Safe to clear
Comes back?
Yes, the cache refills as you run yarn install

What is in this folder?

Yarn Classic downloads each package once into a versioned folder here and copies it into your project. Yarn's documentation says yarn cache clean clears the global cache, which is repopulated the next time you run yarn or yarn install.

Newer Yarn versions (Berry, 2 and later) work differently. They store packages in a .yarn/cache folder inside a project when zero-installs are on, or in a global folder under ~/.yarn/berry. Those project caches may be committed to Git, so check before deleting them.

Is it safe to delete?

Yes for the global cache folder. Everything in it is a copy of a registry download. If you have a project using zero-installs with .yarn/cache committed, that folder is part of your repository, so do not delete it without checking.

How do I clear it by hand?

  1. Stop any running install.
  2. Check where the cache is, then clear it.
  3. Run yarn install in a project when you need packages again.
yarn cache dir
yarn cache clean
You can name a package, as in yarn cache clean <package>, to remove just that one.

How do I stop it growing?

Run yarn cache clean occasionally. Yarn does not cap the global cache on its own.

How Clean House handles it

Clean House recognizes the Yarn cache as downloaded JavaScript packages made by Yarn and marks it as something that comes back. It leaves the folder alone while Yarn is running, and everything it clears goes to the Trash first.

Questions

Is it safe to delete the Yarn cache?

Yes. Yarn refills it from the registry the next time you install.

What does yarn cache clean do?

It clears the global cache. It refills the next time you run yarn install.

Where is the Yarn cache on a Mac?

For Yarn Classic it is ~/Library/Caches/Yarn. Run yarn cache dir to confirm.

Sources

  1. Yarn Classic Docs: yarn cache
  2. Yarn Docs: Offline cache