Open History to see the last commit
1---
2id: MutationCache
3title: MutationCache
4---
5β
6Defined in: [packages/query-core/src/mutationCache.ts:124](https://github.com/TanStack/query/blob/main/packages/query-core/src/mutationCache.ts#L124)
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:129](https://github.com/TanStack/query/blob/main/packages/query-core/src/mutationCache.ts#L129)
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:129](https://github.com/TanStack/query/blob/main/packages/query-core/src/mutationCache.ts#L129)
61β
62## Methods
63β
64### clear()
65β
66```ts
67clear(): void;
68```
69β
70Defined in: [packages/query-core/src/mutationCache.ts:236](https://github.com/TanStack/query/blob/main/packages/query-core/src/mutationCache.ts#L236)
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:278](https://github.com/TanStack/query/blob/main/packages/query-core/src/mutationCache.ts#L278)
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β
128#### Returns
129β
130 \| [`Mutation`](Mutation.md)\<`TData`, `TError`, `TVariables`, `TOnMutateResult`\>
131 \| `undefined`
132β
133#### See
134β
135[MutationCache#findAll](#findall)
136β
137#### Example
138β
139```ts
140const mutationCache = queryClient.getMutationCache()
141β
142const mutation = mutationCache.find({ mutationKey: ['addPost'] })
143```
144β
145***
146β
147### findAll()
148β
149```ts
150findAll(filters: MutationFilters): Mutation<unknown, Error, unknown, unknown>[];
151```
152β
153Defined in: [packages/query-core/src/mutationCache.ts:308](https://github.com/TanStack/query/blob/main/packages/query-core/src/mutationCache.ts#L308)
154β
155An even more advanced method that can be used to get existing mutation instances from the
156cache that match the given filters. If no mutations match, an empty array is returned.
157β
158This is not typically needed for most applications, but can come in handy when needing more
159information about mutations in rare scenarios.
160β
161#### Parameters
162β
163##### filters
164β
165[`MutationFilters`](../interfaces/MutationFilters.md) = `{}`
166β
167#### Returns
168β
169[`Mutation`](Mutation.md)\<`unknown`, `Error`, `unknown`, `unknown`\>[]
170β
171#### See
172β
173[MutationCache#find](#find)
174β
175#### Example
176β
177```ts
178const mutationCache = queryClient.getMutationCache()
179β
180const mutations = mutationCache.findAll({ mutationKey: ['addPost'] })
181```
182β
183***
184β
185### getAll()
186β
187```ts
188getAll(): Mutation<unknown, Error, unknown, unknown>[];
189```
190β
191Defined in: [packages/query-core/src/mutationCache.ts:259](https://github.com/TanStack/query/blob/main/packages/query-core/src/mutationCache.ts#L259)
192β
193Returns all mutations within the cache.
194β
195This is not typically needed for most applications, but can come in handy when needing more
196information about a mutation in rare scenarios.
197β
198#### Returns
199β
200[`Mutation`](Mutation.md)\<`unknown`, `Error`, `unknown`, `unknown`\>[]
201β
202#### Example
203β
204```ts
205const mutationCache = queryClient.getMutationCache()
206β
207const mutations = mutationCache.getAll()
208```
209β
210***
211β
212### hasListeners()
213β
214```ts
215hasListeners(): boolean;
216```
217β
218Defined in: [packages/query-core/src/subscribable.ts:41](https://github.com/TanStack/query/blob/main/packages/query-core/src/subscribable.ts#L41)
219β
220Returns `true` while at least one listener is registered, `false` once they have all unsubscribed.
221β
222#### Returns
223β
224`boolean`
225β
226#### Inherited from
227β
228```ts
229Subscribable.hasListeners
230```
231β
232***
233β
234### subscribe()
235β
236```ts
237subscribe(listener: MutationCacheListener): () => void;
238```
239β
240Defined in: [packages/query-core/src/subscribable.ts:27](https://github.com/TanStack/query/blob/main/packages/query-core/src/subscribable.ts#L27)
241β
242Registers a listener to be called on every update this object notifies about. Returns a function
243that removes the listener again β call it to stop listening. The base class never drops a listener
244on its own, though some subclasses clear all of theirs in `destroy()`.
245β
246#### Parameters
247β
248##### listener
249β
250`MutationCacheListener`
251β
252Called on each update, with whatever the subclass passes to its subscribers.
253β
254#### Returns
255β
256```ts
257(): void;
258```
259β
260##### Returns
261β
262`void`
263β
264#### Example
265β
266```ts
267const unsubscribe = subscribable.subscribe(() => {
268 // react to the update
269})
270β
271unsubscribe()
272```
273β
274#### Inherited from
275β
276```ts
277Subscribable.subscribe
278```