Question: [![enter image description here](https://i.stack.imgur.com/BW3nE.png)](https://i.stack.imgur.com/BW3nE.png)

After adding 10 child I want to delete the first child whenever a new child added in the database.

Answer: 
this may help you

```
rootref.child("messeges").child(YourInnerChildKey).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
      int count = 0;
      String firstNodeId;
      for (DataSnapshot ds : dataSnapshot.getChildren()) {
        if (count == 0) firstNodeId = ds.getKey();
        count++;
      }

      if(count > 10){
         rootref.child("messeges").child(YourInnerChildKey).removeEventListener(this);
         firebase.child(firstNodeId).removeValue();
         rootref.child("messeges").child(YourInnerChildKey).addValueEventListener(this);
      }
}

```
The best way to do this is to use the firebase cloud functions. Cloud functions is like are triggers ready to perform a certain task, such as when an item is created, added, deleted or updated, and much more.

Read about it on:
<https://firebase.google.com/docs/functions/get-started>