Open History to see the last commit
1---
2id: MutationCache
3title: MutationCache
4---
5β
6Defined in: [packages/query-core/src/mutationCache.ts:123](https://github.com/TanStack/query/blob/main/packages/query-core/src/mutationCache.ts#L123)
7β
8The `MutationCache` is the storage for mutations.
9β
10Normally, you will not interact with the `MutationCache` directly and instead use a
11`QueryClient`. You can subscribe to it (inherited from `Subscribable`) to be informed of
12safe/known updates to the cache, such as mutations being added, removed, or updated.
13β
14## Example
15β
16```ts
17const unsubscribe = mutationCache.subscribe((event) => {
18 console.log(event.type, event.mutation)
19})
20```
21β
22## Extends
23β
24- `Subscribable`\<`MutationCacheListener`\>
25β
26## Constructors
27β
28### Constructor
29β
30```ts
31new MutationCache(config?: MutationCacheConfig): MutationCache;
32```
33β
34Defined in: [packages/query-core/src/mutationCache.ts:128](https://github.com/TanStack/query/blob/main/packages/query-core/src/mutationCache.ts#L128)
35β
36#### Parameters
37β
38##### config?
39β
40[`MutationCacheConfig`](../interfaces/MutationCacheConfig.md) = `{}`
41β
42#### Returns
43β
44`MutationCache`
45β
46#### Overrides
47β
48```ts
49Subscribable<MutationCacheListener>.constructor
50```
51β
52## Properties
53β
54### config
55β
56```ts
57config: MutationCacheConfig = {};
58```
59β
60Defined in: [packages/query-core/src/mutationCache.ts:128](https://github.com/TanStack/query/blob/main/packages/query-core/src/mutationCache.ts#L128)
61β
62## Methods
63β
64### clear()
65β
66```ts
67clear(): void;
68```
69β
70Defined in: [packages/query-core/src/mutationCache.ts:262](https://github.com/TanStack/query/blob/main/packages/query-core/src/mutationCache.ts#L262)
71β
72Removes all mutations from the cache.
73β
74#### Returns
75β
76`void`
77β
78#### Example
79β
80```ts
81const mutationCache = queryClient.getMutationCache()
82β
83mutationCache.clear()
84```
85β
86***
87β
88### find()
89β
90```ts
91find<TData, TError, TVariables, TOnMutateResult>(filters: MutationFilters):
92 | Mutation<TData, TError, TVariables, TOnMutateResult>
93 | undefined;
94```
95β
96Defined in: [packages/query-core/src/mutationCache.ts:305](https://github.com/TanStack/query/blob/main/packages/query-core/src/mutationCache.ts#L305)
97β
98A slightly more advanced method that can be used to get an existing mutation instance from
99the cache. If the mutation does not exist, `undefined` is returned.
100β
101This is not typically needed for most applications, but can come in handy when needing more
102information about a mutation in rare scenarios.
103β
104#### Type Parameters
105β
106##### TData
107β
108`TData` = `unknown`
109β
110##### TError
111β
112`TError` = `Error`
113β
114##### TVariables
115β
116`TVariables` = `any`
117β
118##### TOnMutateResult
119β
120`TOnMutateResult` = `unknown`
121β
122#### Parameters
123β
124##### filters
125β
126[`MutationFilters`](../interfaces/MutationFilters.md)
127β
128The filters to match. `exact` defaults to `true`.
129β
130#### Returns
131β
132 \| [`Mutation`](Mutation.md)\<`TData`, `TError`, `TVariables`, `TOnMutateResult`\>
133 \| `undefined`
134β
135The first matching mutation, or `undefined`.
136β
137#### See
138β
139[MutationCache#findAll](#findall)
140β
141#### Example
142β
143```ts
144const mutationCache = queryClient.getMutationCache()
145β
146const mutation = mutationCache.find({ mutationKey: ['addPost'] })
147```
148β
149***
150β
151### findAll()
152β
153```ts
154findAll(filters?: MutationFilters): Mutation<unknown, Error, unknown, unknown>[];
155```
156β
157Defined in: [packages/query-core/src/mutationCache.ts:336](https://github.com/TanStack/query/blob/main/packages/query-core/src/mutationCache.ts#L336)
158β
159An even more advanced method that can be used to get existing mutation instances from the
160cache that match the given filters. If no mutations match, an empty array is returned.
161β
162This is not typically needed for most applications, but can come in handy when needing more
163information about mutations in rare scenarios.
164β
165#### Parameters
166β
167##### filters?
168β
169[`MutationFilters`](../interfaces/MutationFilters.md) = `{}`
170β
171The filters to match. Without filters, every mutation is returned.
172β
173#### Returns
174β
175[`Mutation`](Mutation.md)\<`unknown`, `Error`, `unknown`, `unknown`\>[]
176β
177The matching mutations.
178β
179#### See
180β
181[MutationCache#find](#find)
182β
183#### Example
184β
185```ts
186const mutationCache = queryClient.getMutationCache()
187β
188const mutations = mutationCache.findAll({ mutationKey: ['addPost'] })
189```
190β
191***
192β
193### getAll()
194β
195```ts
196getAll(): Mutation<unknown, Error, unknown, unknown>[];
197```
198β
199Defined in: [packages/query-core/src/mutationCache.ts:285](https://github.com/TanStack/query/blob/main/packages/query-core/src/mutationCache.ts#L285)
200β
201Returns all mutations within the cache.
202β
203This is not typically needed for most applications, but can come in handy when needing more
204information about a mutation in rare scenarios.
205β
206#### Returns
207β
208[`Mutation`](Mutation.md)\<`unknown`, `Error`, `unknown`, `unknown`\>[]
209β
210Every mutation in the cache.
211β
212#### Example
213β
214```ts
215const mutationCache = queryClient.getMutationCache()
216β
217const mutations = mutationCache.getAll()
218```
219β
220***
221β
222### hasListeners()
223β
224```ts
225hasListeners(): boolean;
226```
227β
228Defined in: [packages/query-core/src/subscribable.ts:43](https://github.com/TanStack/query/blob/main/packages/query-core/src/subscribable.ts#L43)
229β
230Returns `true` while at least one listener is registered, `false` once they have all unsubscribed.
231β
232#### Returns
233β
234`boolean`
235β
236`true` if at least one listener is registered.
237β
238#### Inherited from
239β
240```ts
241Subscribable.hasListeners
242```
243β
244***
245β
246### subscribe()
247β
248```ts
249subscribe(listener: MutationCacheListener): () => void;
250```
251β
252Defined in: [packages/query-core/src/subscribable.ts:28](https://github.com/TanStack/query/blob/main/packages/query-core/src/subscribable.ts#L28)
253β
254Registers a listener to be called on every update this object notifies about. Returns a function
255that removes the listener again β call it to stop listening. The base class never drops a listener
256on its own, though some subclasses clear all of theirs in `destroy()`.
257β
258#### Parameters
259β
260##### listener
261β
262`MutationCacheListener`
263β
264Called on each update, with whatever the subclass passes to its subscribers.
265β
266#### Returns
267β
268A function that removes the listener.
269β
270() => `void`
271β
272#### Example
273β
274```ts
275const unsubscribe = subscribable.subscribe(() => {
276 // react to the update
277})
278β
279unsubscribe()
280```
281β
282#### Inherited from
283β
284```ts
285Subscribable.subscribe
286```