Skip to main content
Query filters allow you to target specific queries when performing batch operations like invalidation, refetching, or removal. The filters API provides powerful and flexible query matching.

Filter Properties

The QueryFilters interface supports the following properties:
Source: packages/query-core/src/utils.ts:30

Query Key Filtering

Filter by query key with partial or exact matching:
By default, query key filtering uses partial matching. A query matches if the filter key is a prefix of the query’s key.

Exact Matching

Use exact: true to match only queries with the exact key:

Type Filtering

Filter by query activity status:
1

Active queries

Queries with one or more active observers (mounted components)
2

Inactive queries

Queries in the cache with no active observers
3

All queries

Both active and inactive queries

Stale Filtering

Filter by staleness:

Fetch Status Filtering

Filter by fetch status:

Predicate Filtering

Use custom logic to filter queries:
Predicate functions receive the full Query instance, giving you access to state, options, and meta.

Combining Filters

Combine multiple filter properties:

Using Filters with invalidateQueries

Invalidate specific queries:
Source: packages/query-core/src/queryClient.ts:294

Using Filters with refetchQueries

Refetch matching queries:
Source: packages/query-core/src/queryClient.ts:316

Using Filters with removeQueries

Remove queries from the cache:
Source: packages/query-core/src/queryClient.ts:271

Finding Queries

Find queries in the cache using filters:
Source: packages/query-core/src/queryCache.ts:193

isFetching with Filters

Count currently fetching queries:
Source: packages/query-core/src/queryClient.ts:109

Mutation Filters

Similar filtering for mutations:
Source: packages/query-core/src/utils.ts:57

Real-World Examples

Selective Cache Invalidation

Periodic Cache Cleanup

Smart Refresh

Best Practices

  1. Be specific: Use exact matching when possible to avoid unintended invalidations
  2. Combine filters: Use multiple filter properties for precise targeting
  3. Use predicate sparingly: Predicate functions run on every query, so keep them efficient
  4. Consider type: Invalidating inactive queries may not trigger refetches
  5. Test filters: Verify your filters match the intended queries

See Also