Open History to see the last commit
1---
2id: query-invalidation
3title: Query Invalidation
4---
5β
6Waiting for queries to become stale before they are fetched again doesn't always work, especially when you know for a fact that a query's data is out of date because of something the user has done. For that purpose, the `QueryClient` has an `invalidateQueries` method that lets you intelligently mark queries as stale and potentially refetch them too!
7β
8```js
9// Invalidate every query in the cache
10queryClient.invalidateQueries()
11// Invalidate every query with a key that starts with `todos`
12queryClient.invalidateQueries('todos')
13```
14β
15> Note: Where other libraries that use normalized caches would attempt to update local queries with the new data either imperatively or via schema inference, React Query gives you the tools to avoid the manual labor that comes with maintaining normalized caches and instead prescribes **targeted invalidation, background-refetching and ultimately atomic updates**.
16β
17When a query is invalidated with `invalidateQueries`, two things happen:
18β
19- It is marked as stale. This stale state overrides any `staleTime` configurations being used in `useQuery` or related hooks
20- If the query is currently being rendered via `useQuery` or related hooks, it will also be refetched in the background
21β
22## Query Matching with `invalidateQueries`
23β
24When using APIs like `invalidateQueries` and `removeQueries` (and others that support partial query matching), you can match multiple queries by their prefix, or get really specific and match an exact query. For information on the types of filters you can use, please see [Query Filters](./filters#query-filters).
25β
26In this example, we can use the `todos` prefix to invalidate any queries that start with `todos` in their query key:
27β
28```js
29import { useQuery, useQueryClient } from 'react-query'
30β
31// Get QueryClient from the context
32const queryClient = useQueryClient()
33β
34queryClient.invalidateQueries('todos')
35β
36// Both queries below will be invalidated
37const todoListQuery = useQuery('todos', fetchTodoList)
38const todoListQuery = useQuery(['todos', { page: 1 }], fetchTodoList)
39```
40β
41You can even invalidate queries with specific variables by passing a more specific query key to the `invalidateQueries` method:
42β
43```js
44queryClient.invalidateQueries(['todos', { type: 'done' }])
45β
46// The query below will be invalidated
47const todoListQuery = useQuery(['todos', { type: 'done' }], fetchTodoList)
48β
49// However, the following query below will NOT be invalidated
50const todoListQuery = useQuery('todos', fetchTodoList)
51```
52β
53The `invalidateQueries` API is very flexible, so even if you want to **only** invalidate `todos` queries that don't have any more variables or subkeys, you can pass an `exact: true` option to the `invalidateQueries` method:
54β
55```js
56queryClient.invalidateQueries('todos', { exact: true })
57β
58// The query below will be invalidated
59const todoListQuery = useQuery(['todos'], fetchTodoList)
60β
61// However, the following query below will NOT be invalidated
62const todoListQuery = useQuery(['todos', { type: 'done' }], fetchTodoList)
63```
64β
65If you find yourself wanting **even more** granularity, you can pass a predicate function to the `invalidateQueries` method. This function will receive each `Query` instance from the query cache and allow you to return `true` or `false` for whether you want to invalidate that query:
66β
67```js
68queryClient.invalidateQueries({
69 predicate: query =>
70 query.queryKey[0] === 'todos' && query.queryKey[1]?.version >= 10,
71})
72β
73// The query below will be invalidated
74const todoListQuery = useQuery(['todos', { version: 20 }], fetchTodoList)
75β
76// The query below will be invalidated
77const todoListQuery = useQuery(['todos', { version: 10 }], fetchTodoList)
78β
79// However, the following query below will NOT be invalidated
80const todoListQuery = useQuery(['todos', { version: 5 }], fetchTodoList)
81```