TARIQ ALEED
TARIQ ALEED

Reputation: 79

delete records FROM google firebase realtime database

What is the appropriate code to delete records by Specific value from android studio .
Required: Delete all records If starCount field equal 0.

private void deleteData(String strTitle){


}

enter image description here

Upvotes: 0

Views: 188

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 600006

In order to delete a node you need to know its full path. This means you you'll need to run a query to find the node with starCount equal to 0 and then delete each of them individually.

Something like this:

DatabaseReference ref = FirebaseDatabase.getInstance().getReference("posts");
Query query = ref.orderByChild("starCount").equalTo(0);
query.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot postSnapshot: dataSnapshot.getChildren()) {
            postSnapshot.getRef().removeValue();
        }
    }

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

Upvotes: 2

Related Questions