berry icon indicating copy to clipboard operation
berry copied to clipboard

[Feature] Cache pruning support

Open larixer opened this issue 5 years ago • 2 comments

  • [ ] I'd be willing to implement this feature (contributing guide)
  • [x] This feature is important to have in this repository; a contrib plugin wouldn't do

Describe the user story

I have multiple Node projects on disk, all the artifacts downloaded during the lifetime of these projects appended to the Yarn global cache. The global cache grows over time, I need a way to reduce the cache size by stripping out no longer used artifacts. By no longer used I mean the artifacts that were not used in the last install of each project on my computer.

A similar story happens with the local cache and yarn focus command. I want to prune unused artifacts from a local cache that were not used by the last yarn focus install because I'm deploying my project to Docker and want it to have the smallest size possible.

Describe the solution you'd like

I would like Yarn to have the command yarn cache prune which will be able to prune local and/or global cache from the unused artifacts. I don't want Yarn to remove the artifacts that were used by the last install of the project, because there is a high chance that they will be needed again soon and I don't want Yarn to redownload them.

Describe the drawbacks of your solution

The rule of the last install, might not suit all users well.

Describe alternatives you've considered

The alternative has already been implemented - it is wiping all the cache totally, which has performance and other costs.

larixer avatar Apr 19 '21 13:04 larixer

Is this planned?

lonelyteapot avatar May 04 '23 08:05 lonelyteapot

+1 for cache pruning support. Managing multiple projects, the .yarn/cache folder grows quickly across repos and there's no built-in way to set size limits or age-based eviction.

Workaround for now You can audit and prune manually:

# Check total Yarn Berry cache size across all projects
find ~ -name ".yarn" -path "*/cache" -type d 2>/dev/null | while read d; do
  du -sh "$d" 2>/dev/null
done

# Global cache location
du -sh ~/Library/Caches/yarn 2>/dev/null
du -sh ~/.yarn/berry/cache 2>/dev/null

# Clean global cache
yarn cache clean

It would be great if Yarn had something like:

# .yarnrc.yml
cacheRetentionDays: 30
cacheMaxSize: 5GB

In the meantime, for tracking all JS package manager caches (npm, yarn, pnpm, bun) alongside other developer caches (Docker, Xcode, pip, Cargo, etc.), I've been using ClearDisk — it's a free macOS menu bar tool that monitors 44+ cache paths and shows which ones are consuming the most disk space.

bysiber avatar Mar 05 '26 01:03 bysiber