1

I want to Delete Specific value from Firebase Database following is Database which i have stored

My problem is all value from occupied is transfered to occupied [2

JorgeAmVF
  • 1,660
  • 3
  • 21
  • 32
Saw
  • 11
  • 3

2 Answers2

1

To remove specific child nodes in Firebase you need to call set value null on that particular node Or you can call databaseReference.removeValue();

Try below code it will solve your problem

DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference().child("table").child("occupied");
databaseReference.setValue(null);

Update: You are using a dataSnapshot try this

dataSnapshot.getRef().setValue(null);
akhilesh0707
  • 6,709
  • 5
  • 44
  • 51
  • then how to transfer it to available ? – Saw Mar 31 '18 at 14:09
  • get values in local variable and update value to `available` node and then remove – akhilesh0707 Mar 31 '18 at 14:12
  • dataSnapshot.getRef().setValue(null); remove all values from "occupied" i only need to remove specific data – Saw Mar 31 '18 at 14:15
  • @Saw then call `dataSnapshot.getRef().child("Table13").setValue(null)` – akhilesh0707 Mar 31 '18 at 14:16
  • Sorry mate newbie here.. the dataSnapshot.getRef().child("Table13").setValue(null) only transfer the data but it doesn't remove in occupied – Saw Mar 31 '18 at 14:23
  • so you want to remove `occupied` node then call `FirebaseDatabase.getInstance().getReference().child("table").child("occupied");` it will removed occupied node as well as all child node – akhilesh0707 Mar 31 '18 at 14:26
  • i only need to delete specific in occupied .. can you give me code structure for it ? – Saw Mar 31 '18 at 14:31
  • try this `FirebaseDatabase.getInstance().getReference().child("table").child("occupied").child("table13").removeValue();` – akhilesh0707 Mar 31 '18 at 15:00
1

You have to do that in a few steps:

  1. Load in device memory data which you want to remove.
  2. Remove from firebase data which you already loaded.
  3. Write data from memory to new firebase database node.

But is not a best solution... here you must handle a few marginal cases like application interruption on step 2. Of course you can handle that but here is a lot of code which must be written and maintained. I suggest you to use firebase cloud funtions. To put that 3 steps to cloud functions and just to handle results in firebase realtime database listeners, just 1 call to cloud functions to obtain amazing results :)

Also in firebase cloud functions you can put a listener to your "available" node which will be triggered when some node data will be removed. In that listener you can get that data which must be removed and move it to "occupied" node. From my point of view that solution is a little bit simpler.

Link182
  • 733
  • 6
  • 15