56

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?

Amit Jayant
  • 2,501
  • 2
  • 29
  • 38

7 Answers7

86

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());
    }
});
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
16

this solved my problem

 mPostReference = FirebaseDatabase.getInstance().getReference()
                        .child("quotes").child(mPostKey);
                mPostReference.removeValue();
masokaya
  • 589
  • 4
  • 10
7

If you are using firebase-admin you can simply try this out as

admin.ref(`/users/${userid}`).remove()

and that works for me.

And also do remember to use async and await syntax.

Vlad
  • 8,225
  • 5
  • 33
  • 45
Debdut Saha
  • 71
  • 1
  • 1
6

You can use this code :

onDeletePost(id:string){
return this.http.delete(`https://my-angular8-prjt.firebaseio.com/posts/${id}.json`).subscribe();
}
Amruta Goyal
  • 71
  • 1
  • 1
2

Depending on how and why you are deleting the data you can use these:

// Could store the push key or get it after push
String newPostKey = yourDatabase.child('firebase-test').push({
    something:something
}).key();

// Depends how you get to here
howYouGotHereId.parent().setValue(null);

Firebase Save Data 3.0

theblindprophet
  • 7,767
  • 5
  • 37
  • 55
0

Assume that images is the directory of your firebase database which you want to clear.

private static DatabaseReference mDatabase;
public static void clearData(){
    mDatabase = FirebaseDatabase.getInstance().getReference();
    mDatabase.child("images").setValue(null);
}

Here images is the parent directory of the database. If you want to clear a nested directory (DCIM) inside the images directory so that you can retain the remaining data in it.

In that scenario you can do like this,

mDatabase = FirebaseDatabase.getInstance().getReference();
mDatabase.child("images").child("DCIM").setValue(null);

As a response to the query, Please try to use setValue method with null value setValue(null) to clear the data from firebase database

Codemaker2015
  • 12,190
  • 6
  • 97
  • 81
0

In the case of Firebase admin with python though:

import firebase_admin
from firebase_admin import credentials
from firebase_admin import db
cred = credentials.Certificate("<your certificate>.json")
firebase_admin.initialize_app(cred, {'databaseURL': 'https://<your db>.firebaseio.com/'})

snapshot = db.reference('firebase-test').get()
for k,v in snapshot.items():
    if v['title'] == "Apple":
        db.reference('firebase-test').child(k).delete()
masaya
  • 410
  • 2
  • 9
  • 15