I would like to be able to delete all of a user’s documents when deleting his account. I would like to do this on the backend side with cloud functions.
This similar post doesn’t really answer my question (because I need to delete documents from multiple collections)
Here is the structure of my collections:
users
| uid
| email: [email protected]
orders
| docId
| createdBy: uid
| …
| …
Here is my function:
const userDeleted = functions.auth.user().onDelete(user => {
// get user doc in users collection then remove it
const usersRef = db.collection("users").doc(user.uid);
usersRef.delete();
// get all user’s doc in orders collection
const ordersRef = db.collection("orders")
.where("createdBy", "==", user.uid)
.get()
.then(snapshot => {
snapshot.forEach(doc => {
ordersRef.doc(doc.id).delete();
});
})
.catch(err => {
console.log("Error getting documents", err);
});
return Promise.all(usersRef, ordersRef);
})
I need to iterate through the orders collection and find the documents that have the correct uid in the “createdBy” property and then delete them. I feel like I can’t delete multiple things at once during onDelete, and I couldn’t find anything concrete in the documentation.
What am I doing wrong?