Skip to content

Local firestore cache doesn't track deleted documents #8952

@jordan-dalby

Description

@jordan-dalby

Operating System

All, web based

Environment (if applicable)

Everywhere

Firebase SDK Version

11.4.0

Firebase SDK Product(s)

Firestore

Project Tooling

React (TS)

Detailed Problem Description

Firestore's local cache doesn't offer manual control. I'm using Realtime Database to track updates and deletes in Firestore, since snapshot listeners didn't cover all my use cases. This setup works well for new and updated documents - Firestore caches them correctly. However, when a document is deleted, Firestore doesn't remove it from the local cache, even after calling getDoc to try get Firebase to realise the document is gone.

Steps and code to reproduce issue

Enable the cache with:

export const db = initializeFirestore(app, {
  localCache: persistentLocalCache({
    tabManager: persistentMultipleTabManager(),
    cacheSizeBytes: Infinity,
  })
});

Track RTDB changes and call getDoc on the deleted doc to try get Firestore to realise it doesn't exist anymore:

    const processShiftRemoval = async (snapshot: DataSnapshot) => {
      const shiftId = snapshot.key;
      if (!shiftId) return;

      // Try force refresh the cache
      await getDoc(doc(db, 'shifts', shiftId));

      console.log('[ShiftContext] Processing shift removal:', shiftId);

      // Update all queries to remove this shift
      const queries = queryClient.getQueriesData<any[]>({ queryKey: ['shifts'] });
      queries.forEach(([queryKey, oldData]) => {
        if (!Array.isArray(oldData)) return;

        queryClient.setQueryData(
          queryKey,
          (oldData: any[] = []) => oldData.filter(shift => shift.id !== shiftId)
        );
      });
    };

    onChildRemoved(pathRef, processShiftRemoval, onError);

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs-triagenewA new issue that hasn't be categoirzed as question, bug or feature requestquestion

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions