Last Updated : 23 Jul, 2025
Next.js, a widely used React framework, offers server-side rendering, static site generation, and robust development features. However, cached data in your development environment can sometimes cause issues. Resetting the cache ensures you work with the latest data and code. Let’s explore several methods to reset the development cache in Next.js:
Why Reset the Cache?Before we dive into the methods, let’s understand why resetting the cache is necessary:
Steps:
rm -rf .next
npm run dev
or
2. Clear Node Modules Cacheyarn dev
Issues can arise from the node modules cache. Clearing it can resolve problems related to module resolution.
Steps:
rm -rf node_modules package-lock.json
or if using yarn:
rm -rf node_modules yarn.lock
npm install
or
3. Use next build with --no-cacheyarn install
For production builds, the --no-cache flag ensures Next.js doesn’t use any cached data.
Steps :
4. Clear Browser Cachenext build --no-cache
If your application displays stale data, it might be due to the browser’s cache. Clear it.
Steps:
Cache issues can be related to environment variables. Reset or update them as needed.
Steps:
Add a custom script to delete the .next folder programmatically.
Steps:
1. Create a Script (e.g., clear-cache.js):
JavaScript
const fs = require('fs');
const path = require('path');
const dir = path.join(__dirname, '.next');
fs.rmSync(dir, { recursive: true, force: true });
console.log('.next folder deleted');
2. Add the Script to package.json:
{
"scripts": {
"clear-cache": "node clear-cache.js"
}
}
3. Run the Script:
Conclusionnpm run clear-cache
Clearing the development cache in Next.js is straightforward and essential for maintaining an up-to-date environment. Whether you manually delete the .next folder, clear the node modules cache, use build flags, or clear the browser cache, these steps ensure a smooth development experience in Next.js.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4