Home > other >  Delete user profile in firebase storage delete everything
Delete user profile in firebase storage delete everything

Time:09-17

I have a simple app where user can upload a profile image. The function for upload works. I want a button to delete the user account and at the same time delete the user image from the firebase storage.

This is the simple function to delete the user picture:

  const deleteUserAccount = () => {
    firebase
      .storage()
      .ref(`users/${user.uid}/profile.jpg`)
      .delete()
      .then(() => {
        console.log('image deleted');
      })
      .catch((err) => {
        console.log(err);
      });

if I execute the function the whole "users" folder is deleted not only the one corresponding to the userId of the user. I just want to delete the corresponding {user.uid} folder.

Here my storage rules:

rules_version = '2';
service firebase.storage {
  match /b/{bucket}/o {
    match /{users}/{uid}/{profileImage} {
      allow read, write: if request.auth.uid == uid;
    }
  }
}

Thank you

CodePudding user response:

If there is nothing else but the profile.jpg in the folder, the behavior is actually as expected. Folders in Cloud Storage are auto-created and deleted as files are added and deleted. You can't have a folder without a file in (through the API).

  • Related