Skip to content

Can I execute code when a query is garbage collected? #872

Discussion options

You must be logged in to vote

Five years later, but this is how I just accomplished this very thing in v5:

queryClient.getQueryCache().subscribe((event) => {
  if (
    event.type === 'removed' &&
    ['CanvasCroppedImage', 'CanvasMultiTagImage'].includes(
      event.query.queryKey[0],
    ) &&
    typeof event.query.state.data === 'string' &&
    event.query.state.data.length > 0
  ) {
    URL.revokeObjectURL(event.query.state.data);
  }
});

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@TkDodo
Comment options

Answer selected by TkDodo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants