Skip to content

Clearing Nx Cache

Managing cache is essential for maintaining efficient Nx monorepos. Over time, the Nx cache can grow significantly, potentially consuming several gigabytes of disk space. This article covers various methods to clear or manage your Nx cache effectively.

Default Nx Cache Location

By default, Nx stores its cache in:

sh
./node_modules/.cache/nx/

This directory can grow to multiple gigabytes, especially in large projects with extensive task executions.

Using nx reset Command

The official and recommended way to clear the Nx cache is using the reset command:

sh
nx reset

This command clears both the computation cache and repositions Nx's internal data to ensure consistency. It's the safest method as it handles both cache cleanup and repository state management.

INFO

As of Nx version 15+, nx reset is the preferred method for cache management.

Manual Directory Removal

For immediate cache clearance, you can manually remove the cache directory:

sh
rm -rf ./node_modules/.cache/nx

This approach instantly frees up disk space but doesn't perform any additional repository state management.

Skipping Cache for Specific Runs

Instead of clearing the entire cache, you can skip using cache for specific command executions:

sh
nx run build --skip-nx-cache
nx run test --skip-nx-cache

This approach is useful when you suspect cache corruption for specific tasks but want to preserve the rest of your cache.

Advanced Cache Management

Custom Cache Location

If the default location within node_modules is problematic, you can configure a custom cache directory in your nx.json:

json
{
  "cacheDirectory": "/tmp/.cache/nx"
}

This is particularly useful in CI/CD environments or when you want to store cache in a different location.

Automated Cache Management Script

For automatic cache size management, you can implement a script that clears cache when it exceeds a specified size:

javascript
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('Clearing NX cache due to size limit');
      rimraf.sync(cachePath);
    }
  });
}

WARNING

Automated scripts require additional dependencies (rimraf, get-folder-size) and should be used with caution in production environments.

Best Practices

  • Use nx reset for routine cache management
  • Monitor cache size regularly in large projects
  • Consider custom cache locations in CI/CD pipelines
  • Use --skip-nx-cache for debugging cache-related issues

When to Clear Cache

  • When experiencing inconsistent build results
  • After major dependency updates
  • When disk space becomes limited
  • When switching between significantly different branches
  • After Nx version upgrades

TIP

The Nx cache significantly improves build performance. Only clear it when necessary, as rebuilding the cache will temporarily increase build times.

By understanding these cache management techniques, you can maintain optimal performance while controlling disk space usage in your Nx monorepo.