Skip to content

Set explicit gcTime on QueryClient#1359

Closed
Twixes wants to merge 1 commit intomainfrom
twixes/query-gc-time-default
Closed

Set explicit gcTime on QueryClient#1359
Twixes wants to merge 1 commit intomainfrom
twixes/query-gc-time-default

Conversation

@Twixes
Copy link
Copy Markdown
Member

@Twixes Twixes commented Mar 30, 2026

Problem

We set staleTime globally but never set gcTime. TanStack Query's default gcTime is 5 min which sounds fine, but queries that override with staleTime: Infinity (we have a bunch) stay "fresh" forever, meaning the GC never kicks in for them even when they go inactive.

Changes

Set an explicit gcTime: 30min at the client level so inactive query data always gets cleaned up regardless of staleTime. 30 min is generous enough for a long-running desktop app where you're switching between tasks/files, but prevents multi-hour sessions from hoarding stale data indefinitely.

Without an explicit gcTime, inactive queries using staleTime: Infinity
stay "fresh" forever and never get garbage collected. Setting gcTime
to 30 min ensures inactive query data is eventually cleaned up while
being generous enough for a long-running desktop app.
@Twixes Twixes force-pushed the twixes/query-gc-time-default branch from 1443977 to eba995e Compare March 30, 2026 07:37
Copy link
Copy Markdown
Contributor

@jonathanlab jonathanlab left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

afaik gcTime and staleTime are fully independent. Garbage collection is only triggered once a query has zero observers right? So if a query with staleTime: infinity has no observers, it gets garbage collected after 5 minutes. If it has observers, it'll just stay around infinitely.

I do not see how bumping it to 30 minutes helps in this scenario, as it would only increase cache retention, but I might be wrong :)

@Twixes
Copy link
Copy Markdown
Member Author

Twixes commented Mar 30, 2026

You are right, there's no point changing this, I misunderstood the setting's impact

@Twixes Twixes closed this Mar 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants