70

I have an Nx monorepo (https://nx.dev). It has a folder with Nx cache (./node_modules/.cache/nx/). Its size for now is over 3GB.

Is there a command for clear this cache?

Pax Beach
  • 2,059
  • 1
  • 20
  • 27

5 Answers5

71

Just delete the whole 'nx' cache folder:

rm -rf ./node_modules/.cache/nx
pegaltier
  • 494
  • 4
  • 11
daiscog
  • 11,441
  • 6
  • 50
  • 62
70

nx reset clears the cache.

Docs on nx reset: https://nx.dev/nx/reset#reset

Docs on the cache here: https://nx.dev/using-nx/caching#local-computation-caching

The above answer nx clear-cache is for the jest cache. I would comment but no rep :)

CobyPear
  • 701
  • 3
  • 4
31

This works in the latest version as of today (February 12, 2022). I am uncertain why this is no longer in the CLI documentation despite evidence of it being there in the past: https://nx.dev/cli/clear-cache

nx clear-cache

Joe Chung
  • 11,955
  • 1
  • 24
  • 33
  • 2
    `nx help` prints: `nx reset - Clears all the cached Nx artifacts (...) [aliases: clear-cache]`. But `reset` is not in the CLI documentation either. – isnot2bad Mar 04 '22 at 14:49
  • 1
    [https://nx.dev/cli/reset](https://nx.dev/cli/reset) works so perhaps there was a rename (with backward compatible alias). – Joe Chung Mar 11 '22 at 14:37
  • @JoeChung, thanks for your answer. I tried many examples but couldn't make the build successful due to the cache. after clearing the cache build works fine. – Archin Modi Apr 23 '23 at 19:47
12

There is not really any command to delete the Nx cache except to skip it or use the following command.

npx nx run build --skip-nx-cache

npx nx run test --skip-nx-cache

If size of the directory is your problem then may be running your node script as a cron job might be an option. In case, location of the directory is your concern then you also configure it and move it outside node_modules like this.

saumyajain125
  • 93
  • 1
  • 4
vsr
  • 1,025
  • 9
  • 17
  • 1
    The link for "skip it" doesn't seem to reference anything regarding the ability to skip the cache? – Jacques Jan 24 '22 at 13:04
5

I have implemented such a solution, but do not find it convenient. Perhaps NX has a command to clear its cache, but I did not find it.

package.json

  "scripts": {
    "nx": "nx",
    "postnx": "node checkAndClearCache.js",
  ...

checkAndClearCache.js

const fs = require('fs');
const rimraf = require('rimraf');
const getSize = require('get-folder-size');

const cachePath = 'node_modules/.cache/nx';
const maxCacheMb = 2048;

if (fs.existsSync(cachePath)) {
  getSize(cachePath, (err, size) => {
    if (err) {
      throw err;
    }

    const MBSize = (size / 1024 / 1024).toFixed(2);

    console.log(`*** NX cache size is ${MBSize} Megabytes`);
    if (MBSize > maxCacheMb) {
      console.log('*** CLEAR NX CACHE ***');
      rimraf.sync(cachePath);
    }
  });
}
Pax Beach
  • 2,059
  • 1
  • 20
  • 27