Redux
Advanced
Cache Lifetimes
Control how long RTK Query keeps data in cache with garbage collection strategies.
20 min
2 sections
cache
garbage-collection
performance
1
2
01. Cache Duration
Section 1 of 2
RTK Query removes unused cache entries after a timeout. Configure keepUnusedDataFor to control this.
typescript
export const api = createApi({
keepUnusedDataFor: 30, // Keep for 30 seconds (default)
endpoints: (builder) => ({
getTodos: builder.query({
query: () => '/todos',
}),
// Specific endpoint override
getUser: builder.query({
query: (id) => `/users/${id}`,
keepUnusedDataFor: 300, // 5 minutes
}),
}),
});Back to Course