How to remove single cache entry? I don't find api in the docs of rtk query #5104
Replies: 1 comment
-
|
You don’t need (and can’t really) “manually delete one cache entry” in RTK Query. There’s no supported per-cache-key delete API. More importantly: if the user is deleted, the correct thing is that the query result for
For the detail query, you can set it to const id = deletedUserId;
dispatch(
api.util.updateQueryData('getUserById', id, (draft) => {
// draft is whatever your query returns
// if it returns `User`, you can’t literally delete the entry,
// but you can clear it so the UI stops rendering stale data
return undefined as any;
})
);In practice I usually pair that with navigation away from the detail page (or showing “User deleted”) so you’re not sitting on a route that expects the user to exist. For any “list” queries, remove the item from arrays: dispatch(
api.util.updateQueryData('getUsers', undefined, (draft) => {
const idx = draft.findIndex((u) => u.id === id);
if (idx !== -1) draft.splice(idx, 1);
})
);
Example gating: const skip = userWasDeleted || !id;
const { data } = useGetUserByIdQuery(id, { skip });
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I have an query endpoint which fetches user by id. after removing it, i can't use invalidateTags because it will trigger refetch on my page and it will be backend 404 error. I'm going to remove cache entry manually, but there is no api to doing that
Beta Was this translation helpful? Give feedback.
All reactions