Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete from firebase realtime database?

I am using Firebase realtime database in Android app, and have data like this: enter image description here

How can i delete the record "Apple" (marked in picture)?

According to the docs, to remove an item you call removeValue() on the reference. But to get the reference i require the child id. Because its a random generated id (-KISNx87aYigsH3ILp0D), how to delete it?

like image 597
Amit Jayant Avatar asked May 23 '16 12:05

Amit Jayant


People also ask

How do I remove a value from Firebase Realtime Database?

Callbacks are removed by calling the off() method on your Firebase database reference. You can remove a single listener by passing it as a parameter to off() .

How do you delete data in firebase unity?

To delete a file, first create a reference to that file. Then call the DeleteAsync() method on that reference. // Create a reference to the file to delete.


2 Answers

If you don't know the key of the items to remove, you will first need to query the database to determine those keys:

DatabaseReference ref = FirebaseDatabase.getInstance().getReference(); Query applesQuery = ref.child("firebase-test").orderByChild("title").equalTo("Apple");  applesQuery.addListenerForSingleValueEvent(new ValueEventListener() {     @Override     public void onDataChange(DataSnapshot dataSnapshot) {         for (DataSnapshot appleSnapshot: dataSnapshot.getChildren()) {             appleSnapshot.getRef().removeValue();         }     }      @Override     public void onCancelled(DatabaseError databaseError) {         Log.e(TAG, "onCancelled", databaseError.toException());     } }); 
like image 111
Frank van Puffelen Avatar answered Sep 28 '22 07:09

Frank van Puffelen


this solved my problem

 mPostReference = FirebaseDatabase.getInstance().getReference()                         .child("quotes").child(mPostKey);                 mPostReference.removeValue(); 
like image 24
masokaya Avatar answered Sep 28 '22 08:09

masokaya