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.