0

I am using google Firestore Database for an app i am working on. The database layout is as follows:

User

---email

---name

---etc...

Post

---[User ID]

---image

---etc...

When i delete a user, i want all posts that reference the users ID to also be deleted. I am using Google Firestore Database alongside Flutterflow, and the users will be deleting their account from the app that i am building.

I have tried to use the Delete User Data extension but i could not configure it correctly to work. Perhaps it is not made for this scenario?

Renaud Tarnec
  • 79,263
  • 10
  • 95
  • 121

1 Answers1

1

I have tried to use the Delete User Data extension but i could not configure it correctly to work. Perhaps it is not made for this scenario?

New answer

The extension deletes docs associated to a specific user ID when the user is deleted from Firebase Authentication. In your case you want to delete documents from a collection when a (master) document is deleted in another collection.

One possibility in your case would be to use a background triggered Cloud Function that is triggered when a document is deleted from the User Firestore collection and that deletes all the corresponding docs in the Post collection.

Something along the following lines:

exports.deleteUsers = functions
    .firestore
    .document('User/{docId}')
    .onDelete(async (snap, context) => {

        try {

            const db = admin.firestore();
            let batch = db.batch();

            const querySnapshot = await db.collection("Post").where("ArtistReference", "==", context.params.docId).get();

            querySnapshot.forEach((doc) => {
                batch.delete(doc.ref);
            });

            return batch.commit();

        } catch (error) {
            //...
        }

    });

Note that a batched write can only contain up to 500 operations. If you have cases with more than 500 "Post" docs corresponding to a user, you should use Promise.all().


Erroneous answer

(The extension deletes docs associated to a specific user ID when the user is deleted from Firebase Authentication and not when another Firestore doc is deleted)

As explained in the extension documentation, in your case (a document field is used to associate the user UID with the document) you need to enable the auto discovery and configure the extension as follows:

Auto discovery search fields

If auto discovery is enabled, specify what document fields are used to associate the UID with the document. The extension will delete documents where the value for one or more of these fields matches the deleting user’s UID.

Renaud Tarnec
  • 79,263
  • 10
  • 95
  • 121
  • So the field's name is 'ArtistReference'. I added this next to the other fields (predetermined by the extension) separated with a comma. Then i reconfigured the extension and tested it by adding an image by myself, then deleting my account, but this did not delete the entire post, nor did it remove the reference from the post. – Ben Sturdy May 10 '23 at 12:45
  • Ok my mistake. The extension deletes docs assoicted to a specific user ID **when the user is deleted from Firebase Authentication**. I'll adapt my answer. – Renaud Tarnec May 10 '23 at 14:21