Clean Your Yarn Cache!

27 Oct 2020

One thing people may not realize, even if they’re seasoned frontend devs - your Yarn cache is bigger than you think. Have you ever looked at your Yarn cache?

A few months ago at work, my laptop’s 500GB SSD was mysteriously nearly full - I didn’t understand how that was possible! I only had about 1.5GB of my own stuff, as far as I knew.

Long story short, my Yarn cache directory was huge. Please, take a moment and guess how big you think a Yarn cache could be.

Yarn stores every package in a global cache in your user directory on the file system.

Before I answer how big my Yarn cache is, let’s check yours out!

To get the current size of your Yarn cache, run the following in your terminal: (it may take quite a while!)

du -hcs $(yarn cache dir)

Want to get rid of your enormous cache? Just run the following:

yarn cache clean

But… it’s not always that simple.

If your Yarn cache is massive (easy to do!), you will encounter a heap error.

<--- JS stacktrace --->

==== JS stack trace =========================================

    0: ExitFrame [pc: 0x7a9103c70eb]
Security context: 0x1c25f4f31a41 <JSObject>
    1: parse [0x1c25c0e24c99] [pc=0x7a9104ec2de](this=0x1c25fcd8fd01 <Minimatch map = 0x1c25b5ee8641>,0x1c25fcd90001 <String[12]: node_modules>,10)
    2: arguments adaptor frame: 3->2
    3: map [0x1c25f4f34229](this=0x1c25fcd900e9 <JSArray[13]>,0x1c25c0e24c99 <JSFunction parse (sfi = 0x1c25d1efc001)>...

FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory

The only effective solution I found is to run the following:

 rm -rf $(yarn cache dir)

This can potentially take hours (yes, really).

Especially when your Yarn cache is a whopping 324GB (as mine was!)

Even after deleting my cache in August, it’s only two months later now, and it’s back up to 268GB