node.jsgoogle-cloud-firestoregoogle-cloud-functionsfirebase-adminfirebase-tools

Firebase Cloud Functions: Document deletion not working properly and execution timing issues


I have two Firebase Cloud Functions with onDocumentCreated triggers. The issue is that when my delete function tries to delete a document that doesn't exist, it somehow triggers the checker function, which should only fire when documents are created in the userData collection. This cross-triggering behavior is causing unexpected side effects in my application.

These functions are run in Firebase emulator not production.

Function 1

// Attempting to delete a document that may not exist
export const deleteUserData = onDocumentCreated(
  {
    document: "deleteUser/{userId}",
  },
  async (event) => {
    const db = firebase_admin.firestore();
    const tokenDocRef = db.collection("userData").doc(userId);
    tokenDocRef.delete();
  }
);

Function 2

// This should only trigger when documents are created in userData collection
export const checker = onDocumentCreated(
  {
    document: "userData/{userId}",
  },
  async (event) => {
   console.log('triggered')
  }
);

Issues I'm Facing

  1. Unexpected cross-triggering: When a document is created in deleteUser/{userId}, my deleteUserData function correctly triggers and attempts to delete a document from the "userData" collection.
  2. The main problem: If the document being deleted doesn't exist, the checker function (which should only trigger on userData/{userId} document creation) gets triggered unexpectedly.
Execution logs showing the issue:
Beginning execution of "asia-south1-deleteUserData"
Finished "asia-south1-deleteUserData" in 5.352708ms
Beginning execution of "asia-south1-checker"  // This shouldn't happen!
Finished "asia-south1-checker" in 5.352708ms

Environment

  1. Firebase Functions v2
  2. Node.js
  3. Firebase Admin SDK
  4. Firebase Emulator

An obvious way to fix this is to fetch the document , check if it exist and return if it doesn't exist . But I am trying to understand why is this happening in the first place.

  1. Why does deleting a non-existent document trigger the checker function? This seems like it shouldn't happen since no document is actually being created in the fcmTokens collection.
  2. Is there something wrong with my trigger configuration? Both functions have different document paths, so they should be isolated from each other.

Solution

  • Confirmed by the Firebase team, it was a bug. My submitted report has been accepted.

    Issue 8845