You can delete multiple documents in a collection by calling the
delete() method on an object collection or a query builder.
To delete multiple documents, pass a query filter to the where()
method. Then, delete the matching documents by calling the delete()
method.
Tip
To learn more about deleting documents with the Laravel Integration, see the Delete Documents section of the Write Operations guide.
Example
Select from the following Eloquent and Query Builder tabs to view usage examples for the same operation that use each corresponding query syntax:
This example performs the following actions:
Uses the
MovieEloquent model to represent themoviescollection in thesample_mflixdatabaseDeletes documents from the
moviescollection that match a query filterPrints the number of deleted documents
The example calls the following methods on the Movie model:
where(): Matches documents in which the value of theyearfield is less than or equal to1910delete(): Deletes the matched documents and returns the number of documents successfully deleted
$deleted = Movie::where('year', '<=', 1910) ->delete(); echo 'Deleted documents: ' . $deleted;
Deleted documents: 7
This example performs the following actions:
Accesses the
moviescollection by calling thetable()method from theDBfacadeDeletes documents from the
moviescollection that match a query filterPrints the number of deleted documents
The example calls the following query builder methods:
where(): Matches documents in which the value of theyearfield is less than or equal to1910delete(): Deletes the matched documents and returns the number of documents successfully deleted
$deleted = DB::table('movies') ->where('year', '<=', 1910) ->delete(); echo 'Deleted documents: ' . $deleted;
Deleted documents: 7
To learn how to edit your Laravel application to run the usage example, see the Usage Examples landing page.