# On-Demand Revalidation (revalidatePath)

> Instantly purge cached data for a specific route after a mutation.

Subject: Next.js | Lesson 27 of 65 | 6 min read
Interactive lesson: https://codewithanimation.in/nextjs/revalidate-path
Previous: [Time-based Revalidation (ISR)](https://codewithanimation.in/nextjs/time-based-revalidation.md) | Next: [On-Demand Revalidation (revalidateTag)](https://codewithanimation.in/nextjs/revalidate-tag.md)

## Surgical Cache Clearing

`revalidatePath` is a Next.js utility function that allows you to manually purge the Data Cache and Full Route Cache for a specific route path.

Unlike time-based revalidation (ISR) which relies on a ticking clock, on-demand revalidation happens exactly when you tell it to.

## Immediate Consistency

When a user submits a form, creates a new post, or edits their profile, they expect to see the updated data immediately.

If you relied on time-based revalidation, they might have to wait 60 seconds. By calling `revalidatePath` immediately after your database mutation, Next.js dumps the stale cache and ensures the next request fetches fresh data.

## Inside Server Actions

You typically call `revalidatePath` inside a Server Action or a Route Handler.

For example: `await db.post.create(data);` followed by `revalidatePath('/blog');`. The next time someone visits `/blog`, they will see the new post.

## Common mistake: The Dynamic Route Gotcha

A massive pain point for developers: `revalidatePath('/blog')` ONLY clears the exact route `/blog`. It does NOT clear dynamic children like `/blog/123`.

If you want to clear a route and ALL of its nested children, you must pass a second argument: `revalidatePath('/blog', 'layout')`. This tells Next.js to purge the layout and everything underneath it.

## Tip: The Nuclear Option

If you ever need to completely flush the cache for your entire application, you can call `revalidatePath('/', 'layout')`.

However, this is highly inefficient and will cause a temporary spike in database load as every route rebuilds. Always aim for surgical, targeted revalidation.

## Try it: The Revalidation Laser

Interact with the Server Action below. Notice how calling `revalidatePath('/blog')` surgically targets and vaporizes only that specific cache node, while leaving the rest of the application's cache untouched.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/nextjs/revalidate-path

## Check yourself

### Question 1

You have a blog index at `/blog` and individual posts at `/blog/[id]`. You call `revalidatePath('/blog')`. What happens?

- A. Both the index and all individual posts are cleared
- B. Only the /blog index is cleared
- C. Only the individual posts are cleared
- D. The entire application cache is cleared

Correct answer: B

By default, `revalidatePath('/blog')` only purges the cache for that exact URL. To include the child routes, you must use `revalidatePath('/blog', 'layout')`.

- Why not A: By default, revalidatePath is exact-match only.
- Why not C: The exact path passed is `/blog`.
- Why not D: That would require `revalidatePath('/', 'layout')`.

### Question 2

When is the best time to use `revalidatePath`?

- A. For data that updates steadily every 5 minutes
- B. Inside a Client Component inside a useEffect
- C. Immediately after performing a database mutation in a Server Action
- D. Inside a layout.tsx file to ensure fresh data on every load

Correct answer: C

`revalidatePath` is designed for on-demand cache invalidation, making it the perfect tool to call directly after you update your database via a Server Action or Route Handler.

- Why not A: Time-based revalidation (ISR) is better suited for predictable intervals.
- Why not B: revalidatePath is a server-side only function.
- Why not D: You cannot call mutations during the rendering phase.

## Remember this

- `revalidatePath('/path')` clears the cache on-demand.
- It is ideal for mutations (forms, updates, deletes) requiring immediate consistency.
- By default, it ONLY clears the exact path specified.
- Use `revalidatePath('/path', 'layout')` to clear the path AND all its children.
- Call it from Server Actions or Route Handlers.
