You are currently viewing documentation for the canary channel of Next.js.
expireTag
expireTag
allows you to purge cached data on-demand for a specific cache tag.
Good to know:
expireTag
is available in both Node.js and Edge runtimes.expireTag
only invalidates the cache when the path is next visited. This means callingexpireTag
with a dynamic route segment will not immediately trigger many expirations at once. The invalidation only happens when the path is next visited.
Reference
Parameters
expireTag(...tags: string[]): void;
tags
: String arguments representing the cache tags associated with the data you want to revalidate. Must be less than or equal to 256 characters each. This value is case-sensitive.
You can add tags to fetch
as follows:
fetch(url, { next: { tags: [...] } });
Returns
expireTag
does not return a value.
Examples
Server Action
You can invoke expireTag
in a Server Action:
app/actions.ts
'use server'
import { expireTag } from 'next/cache'
export default async function submit() {
await addPost()
expireTag('posts', 'blog')
}
Route Handler
You can invoke expireTag
in a Route Handler:
app/api/revalidate/route.ts
import type { NextRequest } from 'next/server'
import { expireTag } from 'next/cache'
export async function GET(request: NextRequest) {
const tag = request.nextUrl.searchParams.get('tag')
expireTag(tag)
return Response.json({ revalidated: true, now: Date.now() })
}
Was this helpful?