Your Search Bar For Shrewd Tips

How Do I Clear Ng Cache?


How Do I Clear Ng Cache?

If an Angular project is building slowly, showing outdated changes, or behaving differently after an update, clearing the Angular CLI cache may help. The quickest way is to open a terminal in your Angular workspace and run ng cache clean. This removes the persistent build cache so Angular can create a fresh one the next time you build or serve the application.

Clearing the cache is generally safe: it does not delete your source code or project configuration. Your next build may take longer because Angular has to do more work without cached results. Below, you’ll find the command, where the cache is stored, how to clear it manually, and what to try if the problem continues.

The Quick Answer: Run ng cache clean

From a terminal, navigate to the root folder of your Angular workspace—the folder that contains angular.json—and run:

ng cache clean

In some projects, the Angular CLI is installed locally rather than globally. If your terminal does not recognize ng, try running the command through your package manager:

  • npx ng cache clean for an npm project
  • pnpm exec ng cache clean for a pnpm project
  • yarn ng cache clean for a Yarn project

You can also use the project’s local Angular CLI executable directly, although the package-manager commands are usually more convenient. Once the command finishes, rerun your usual build or development-server command, such as ng build or ng serve.

The CLI’s cache-cleaning command removes Angular’s persistent disk cache. It does not clear every kind of cache on your computer, and it does not reinstall project dependencies. If your issue comes from npm, a browser, a service worker, or a development server, you may need a different fix.

What Is the Angular Cache?

The Angular CLI uses a persistent cache to save work between builds. When it can reuse previously calculated results, tasks such as compiling or processing parts of an application may finish faster. The cache is especially useful during local development, when you repeatedly serve or build the same project.

Cached data is generated by the tooling; it is not your application’s source code. Clearing it means Angular must regenerate that data. The first build after cleanup can therefore be slower than usual, while later builds may speed up again.

The Angular cache is separate from several other things developers sometimes call “the cache.” For example, npm or pnpm may cache downloaded packages, a browser may keep old web assets, and a service worker may store files for an installed application. Running ng cache clean targets the Angular CLI cache, not all of those other stores.

Where Does Angular Store Its Cache?

In many Angular workspaces, the persistent cache is stored in a .angular/cache directory under the workspace root. A typical project might have a structure like this:

my-angular-app/.angular/cache/

The exact location can vary if the workspace has been configured to use a different cache path. To inspect the cache settings and status supported by your installed Angular CLI version, run:

ng cache info

Run this command from the workspace root, just as you would for ng cache clean. If you are unsure where the workspace root is, look for the directory containing angular.json. Running Angular CLI commands from an unrelated folder can result in a workspace error or cause the command to use different settings.

How to Clear Angular Cache Manually

The recommended approach is to use ng cache clean, because it asks the Angular CLI to clear its own cache. If the command is unavailable, or you need to remove the cache folder manually, close any active Angular build or development-server processes first. Then delete the workspace’s .angular/cache directory.

On macOS or Linux, from the workspace root, you can run:

rm -rf .angular/cache

On Windows Command Prompt, use:

rmdir /s /q .angular\cache

In PowerShell, use:

Remove-Item -Recurse -Force .angular\cache

These commands permanently remove the specified directory, so check that your terminal is in the correct project before running them. If your workspace uses a custom cache path, remove that configured directory instead. When in doubt, prefer ng cache clean and check the project’s Angular CLI configuration before deleting files manually.

How to Disable or Re-Enable the Angular Cache

If you are investigating a problem that returns after every build, you may want to temporarily disable the cache rather than repeatedly clearing it. Angular CLI versions that support cache controls provide these commands:

  • ng cache disable turns off the Angular CLI cache.
  • ng cache enable turns the cache back on.
  • ng cache info displays cache information for the workspace.
  • ng cache clean removes the existing cached data.

These controls are useful for troubleshooting, but leaving the cache disabled can make repeated builds slower. After testing, re-enable it if you want to benefit from persistent caching. If a command is not recognized, check your Angular CLI version and consult the help output available in your project, for example ng cache --help.

You can also review the cache configuration in angular.json. Depending on the CLI version and workspace setup, cache settings can control whether caching is enabled, which environment it applies to, and where its files are stored. Avoid changing configuration simply to fix a one-time build issue; clean the cache first and adjust settings only if you have a specific reason.

When Should You Clear the ng Cache?

Clearing the Angular cache is a reasonable troubleshooting step when build output appears stale, a build fails unexpectedly after a tool or configuration change, or the development environment behaves inconsistently. It can also help when investigating a problem that cannot be reproduced reliably with the cache enabled.

However, cache cleanup is not a routine maintenance task that you need to perform after every build. Angular uses the cache to save time. Clearing it without a reason removes useful data and makes the next build do extra work.

Consider clearing the cache after you have tried simpler checks, or when the error points toward stale build artifacts. If the same error returns after a clean build, the cause may be in your code, dependencies, configuration, or installed tool versions rather than the cache.

What to Do If Clearing the Cache Does Not Fix the Problem

If ng cache clean completes but your Angular app still has the same problem, work through these checks one at a time. Changing several things at once can make it harder to identify the cause.

  • Confirm the workspace: Run the command from the directory containing the intended project’s angular.json file.
  • Restart the development server: Stop ng serve with Ctrl+C, then start it again. A running process may still be holding old state.
  • Check the error message: Look for missing packages, TypeScript errors, invalid configuration, or version conflicts. Clearing a cache cannot repair these issues.
  • Verify your Angular CLI version: Run ng version and make sure the local CLI and project dependencies are compatible.
  • Install dependencies only when appropriate: If packages are missing or inconsistent, follow your team’s package-manager workflow. Do not delete the lockfile casually; it helps keep installations consistent.
  • Check other caches separately: If the problem is only visible in a browser, try a hard refresh or clear that browser’s stored site data. If the app uses a service worker, investigate its update and cache behavior.

For a dependency-related issue, reinstalling packages may be appropriate, but it is a separate step from clearing the Angular cache. Use the package manager and lockfile already chosen by the project. For example, avoid switching between npm, pnpm, and Yarn without a reason, since that can introduce new dependency differences.

If you are working in a continuous integration environment, check whether the build system has its own cache configuration. Clearing .angular/cache locally will not necessarily remove cached data stored by a CI provider. Follow the provider’s instructions to invalidate its cache, and ensure the build is using the expected Node.js and Angular versions.

Common Questions About Clearing Angular Cache

Will clearing ng cache delete my code? No. The Angular CLI cache contains generated, reusable build data. The ng cache clean command is intended to remove that data, not your application files, dependencies, or configuration.

Will the next build be slower? It may be. Angular has to regenerate information that was previously cached. Later builds can become faster again as the cache is populated.

Does ng cache clean clear npm’s cache? No. Angular CLI caching and npm’s package-download cache are separate. If you are troubleshooting a package installation issue, diagnose that issue separately rather than assuming the Angular cache command will fix it.

Can I delete the entire .angular folder? The cache is commonly inside .angular/cache, but the parent directory may contain other Angular workspace data. Prefer ng cache clean or remove only the cache directory unless you have confirmed that the rest of the folder is safe to delete.

Why does ng cache clean fail? Common reasons include using an older CLI that does not support the command, running it outside an Angular workspace, or invoking a different CLI than the one installed in the project. Try the project-local CLI with your package manager and check ng version.

Conclusion

To clear the Angular CLI cache, open a terminal in your workspace root and run ng cache clean. If the global ng command is unavailable, try npx ng cache clean, pnpm exec ng cache clean, or the equivalent command for your package manager. The cache is commonly stored in .angular/cache, but you can check your workspace configuration if the location differs.

Clearing the cache is a safe and useful troubleshooting step, but it does not clear npm’s cache, browser data, or service-worker storage. If a clean Angular cache does not resolve the issue, check the actual error, restart the development server, and investigate dependencies or configuration. Use cache cleanup when it helps—not as a replacement for identifying the underlying problem.


Disclaimer: Articles are written by Humans, AI or Both. Verify Important information.

Shrewdnia

Shrewdnia

Shrewdnia is a destination for curious minds seeking clarity, knowledge, and informed perspectives. Through insightful articles and practical guides our passionate team explores a wide range of topics designed to help readers understand the world around them, make smarter decisions, and stay informed in an ever-changing landscape.


💡 Every question sparks discovery, and every perspective enriches the conversation. Share your thoughts and insights in the comments 👇

Back to blog

Leave a comment

JOIN THE SHREWDNIA COMMUNITY FORUM

What do you think?

Have an opinion, experience, or question about this topic? Join the Shrewdnia Forum and share your thoughts with other readers.

Join the Forum →