简体   繁体   中英

Remove users storage folder with firebase cloud functions

When a user deletes their account, I want to remove their storage files along with their data.

I am able to do a multi path delete for the RTDB, how can I do this but also remove files from storage too?

I have tried chaining on a .then but it makes everything fail...

ex...

.then(() => {
  const bucket = gcs.bucket(functions.config().firebase.storageBucket);
  const path = `categories/${uid}`;
  return bucket.file(path).delete();
})

I wish it was faster to test your functions without always deploying because it has taken soooo much time to try making this work...

Here is my working code:

exports.removeUserFromDatabase = functions.auth.user()
  .onDelete(function(user, context) {
    var uid = user.uid;

    const deleteUserData = {};
    deleteUserData[`users/${uid}`] = null;
    deleteUserData[`feed/${uid}`] = null;
    deleteUserData[`friends/${uid}`] = null;
    deleteUserData[`profileThumbs/${uid}`] = null;
    deleteUserData[`hasUnreadMsg/${uid}`] = null;
    deleteUserData[`userChatRooms/${uid}`] = null;
    deleteUserData[`userLikedPosts/${uid}`] = null;
    deleteUserData[`userLikedStrains/${uid}`] = null;

    return admin.database().ref('/friends').orderByChild(`${uid}/uid`).equalTo(uid)
    .once("value").then((friendsSnapshot) => {
      friendsSnapshot.forEach((friendSnapshot) => {
      deleteUserData[`/friends/${friendSnapshot.key}/${uid}`] = null;
    });
    return admin.database().ref().update(deleteUserData)
    })
    .then(() => {
      // const bucket = gcs.bucket(functions.config().firebase.storageBucket);
      const bucket = admin.storage().bucket();
      const path = `categories/${uid}`;
      return bucket.file(path).delete();
    })
  });

I feel like it's because I am not dealing with the promise correctly, I just don't know where this is going wrong.

My code snippet currently works until i chain the .then()

Cheers.

Your current code is not returning anything from the top-level code, meaning it may get terminated at any point while it's writing to the database.

You'll want to return admin.database()... and then chain the additional then() after it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM