Home > Software design >  How can I delete specific data from each user in firebase
How can I delete specific data from each user in firebase

Time:05-06

I was keeping the scores of users in the users table. But it is no longer needed. So I want to delete the point of each user. How can I do that ?

enter image description here

CodePudding user response:

There is no magic here. You'll have to read all users, loop over them, and then delete their points one-by-one, or through a multi-path update.

Something like:

DatabaseReference usersRef = FirebaseDatabase.getInstance().getReference("UsersExample");
usersRef.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        Map<String, Object> updates = new HashMap<>();
        for (DataSnapshot userSnapshot: dataSnapshot.getChildren()) {
            updates.put(userSnapshot.getKey() "/point", null); // Setting to null will delete that node
        }
        userRef.updateChildren(updates);
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        throw databaseError.toException();
    }
}
  • Related