Skip to content

[RTK Query] removeQueryResult deletes entry instead of resetting to uninitialized, breaking isLoading semantics #5300

@ryansmartpadpro

Description

@ryansmartpadpro

Problem

When RTK Query removes a cache entry via removeQueryResult (such as when invalidating a tag with zero subscribers), it completely deletes the query cache entry from state. This has an unintended side-effect:

  • When a component resubscribes to that query, the query slice has no cache entry, so the isLoading state does not correctly transition through isUninitialized -> isLoading = true -> data.
  • Instead, it acts as if the query never existed; the state is not reset to uninitialized, it's just gone.

Why it matters

This breaks the following:

  • isLoading is meant to only be true when no data exists and a fetch is in-flight (page is loading from empty state)
  • isFetching is meant for showing refetch/loading with already existing data

If a query entry is just deleted and not reset, subscribers cannot differentiate between a fresh initial loading state and a refresh/re-fetch. This can break loading-indicator logic and UI expectations.

Code reference

// buildSlice.ts
removeQueryResult: {
  reducer(
    draft,
    {
      payload: { queryCacheKey },
    }: PayloadAction<QuerySubstateIdentifier>,
  ) {
    delete draft[queryCacheKey]
  },
  ...
}

What should happen instead:

  • reset the entry to status STATUS_UNINITIALIZED (not delete), so that the next subscription triggers the expected state flow

Steps to reproduce

  1. Create a query and subscribe to it (shows isLoading=true on initial mount)
  2. Invalidate the query with no active subscribers (removeQueryResult fires)
  3. Subscribe again. The entry is missing from state, so initial transitions do not occur and isLoading is not true as expected.

Expected behavior:
Deleting a query result when there are no subscribers should reset the entry to uninitialized (not delete), ensuring the correct isLoading/isFetching state transitions.

Links/References

Labels

RTK-Query, bug

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs ReproductionReport is waiting on a reproductionRTK-QueryIssues related to Redux-Toolkit-Query

    Type

    No fields configured for Bug.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions