I'm new to Firebase/Firestore and trying to create a Firebase Function that will delete all user data upon deletion of an Auth account.
My functions is successfully called on deleting an account and I'm trying to delete a collection called links for that user and then delete the user document. But I'm getting an error of linksRef.forEach is not a function.
Any guidance on how I'd do this cascading delete?
exports.deleteUserData = functions.auth.user().onDelete((event) => {
const userId = event.data.uid;
const store = admin.firestore();
store.collection('users').doc(userId).get().then(user => {
if (user.exists) {
user.collection('links').get().then(links => {
links.forEach(link => {
link.delete();
})
return;
}).catch(reason => {
console.log(reason);
});
user.delete();
return;
}
else {
// User does not exist
return;
}
}
).catch(reason => {
console.log(reason);
});
});
Based on this official doc from Firebase, it seems that it's quite easy to set-up a
clearData
function.It only supports basic Firestore structure. But in your case it should work configuring only the
user_privacy.json
with something like:For more complex cases, the function code needs to be tweaked.
Please refer to the doc
Based on the comment from @Doug Stevenson regarding Promises I managed to get this working by scraping together code. Definitely not the cleanest but it works if anyone is trying to do similar.