Delete a Document
On this page
You can delete a document in a collection by retrieving a single Eloquent model and calling
the delete()
method, or by calling delete()
directly on a query builder.
To delete a document, pass a query filter to the where()
method, sort the matching documents,
and call the limit()
method to retrieve only the first document. Then, delete this matching
document by calling the delete()
method.
Example
This usage example performs the following actions:
Uses the
Movie
Eloquent model to represent themovies
collection in thesample_mflix
databaseDeletes a document from the
movies
collection that matches 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 thetitle
field is"Quiz Show"
orderBy()
: sorts matched documents by their ascending_id
valueslimit()
: retrieves only the first matching documentdelete()
: deletes the retrieved document
$deleted = Movie::where('title', 'Quiz Show') ->orderBy('_id') ->limit(1) ->delete(); echo 'Deleted documents: ' . $deleted;
To learn how to edit your Laravel application to run the usage example, see the Usage Examples landing page.
Tip
To learn more about deleting documents with Laravel MongoDB, see the Deleting Models section of the Laravel documentation.