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.
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
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"
limit()
: Retrieves only the first matching documentdelete()
: Deletes the retrieved document
$deleted = Movie::where('title', 'Quiz Show') ->limit(1) ->delete(); echo 'Deleted documents: ' . $deleted;
Deleted documents: 1
This example performs the following actions:
Accesses the
movies
collection by calling thetable()
method from theDB
facadeDeletes a document from the
movies
collection that matches a query filterPrints the number of deleted documents
The example calls the following query builder methods:
where()
: Matches documents in which the value of thetitle
field is"Quiz Show"
limit()
: Retrieves only the first matching documentdelete()
: Deletes the retrieved document
$deleted = DB::table('movies') ->where('title', 'Quiz Show') ->limit(1) ->delete(); echo 'Deleted documents: ' . $deleted;
Deleted documents: 1
To learn how to edit your Laravel application to run the usage example, see the Usage Examples landing page.