Reputation: 108
I'm currently working on a project and I need to be able to delete from a database I've already created. I created it using the .push() to the database hence a unique key is created and thus needed for delete operations.
I tried using the answer from Frank Van Puffelen here How to delete from firebase realtime database?, but I ran into a bug where if two nodes have the same title they'll be deleted.enter image description here
The image shows how my Firebase database looks:
A bit of assistance or direction to an answer would go a long way. Thanks
Upvotes: 2
Views: 248
Reputation: 138969
If you want to delete a single element from your database, you need to know the path to that element, which includes also that pushed key. Your reference should look like this:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference keyRef = rootRef.child("-KlSNx87aYigsH3lLp0D");
keyRef.removeValue().addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
Log.d("TAG", "Element removed successfully!");
}
}
});
Otherwise, you can use a query that looks exactly like this:
Query idQuery = rootRef.orderByChild("id").equalTo(1);
In this case, Frank Van Puffelen's answer from the following post:
Will work perfectly fine.
Upvotes: 1