Chapter 12 - Delete data from Firebase using Laravel PHP

In this chapter, we will learn how to delete the data from Firebase Cloud Firestore database collection using Laravel Framework PHP SDK.

Now consider the following list of students in the 'Student' collection


Suppose you want to delete the Student by document id '8suVf4FphzZLuTFJtdFe', you have to fire the below query.

 app('firebase.firestore')->database()->collection('Student')->document('8suVf4FphzZLuTFJtdFe')->delete();  

This will delete the Student data from Student collection.

Now if you have to delete the student having id = 3, then this can be done by the following query

 $students = app('firebase.firestore')->database()->collection('Student')->where('id','==',3)->limit(1)->documents();  
 $students ->rows()[0]->delete();  

Now consider if you have to delete students having 'passoutyear' less than 2000


This can be done by following query

 $students = app('firebase.firestore')->database()->collection('Student')->where('passoutyear','<',2000)->documents();  
 if($students ->size() > 0) {  
   foreach($students as $stu){  
    if($stu->exists()){  
      $stu->delete();  
    }  
   }  
 }  

Now suppose you have to delete the whole collection 'Student'. Unfortunately, there is no query to delete collection in one go. This is for safety purpose not to lose important data. Also you can see, when you delete collection from Firebase console, it makes you write the collection name correctly as confirmation.


For this, you can loop through each document and delete them individually. If there are more data, you have to use batch.

There is something important you should know with delete of Firebase document. That deleting a document will not delete its documents in the collection inside. Let us show you the example.

Consider, each student document has 'Exams' collection. Now if you delete student 'UTkZTSq9rkv77k5an3V8'.

Once its deleted, just add again 'UTkZTSq9rkv77k5an3V8', you will see 'Exams' for few seconds. Then it will be deleted, because it consider that a new one is added.

Hope you now know how to delete the data from Firebase Cloud Firestore data from Laravel PHP code using the admin package. If you have any doubt or need any chapter on particular topic, please let us know in the comment section.

Post a Comment

2 Comments