Overview
In this guide, you can learn how to delete documents from a MongoDB collection by using the Laravel Integration. Use delete operations to remove data from your MongoDB database.
This section provides examples of the following delete operations:
Sample Model
The operations in this guide reference the following Eloquent model class:
namespace App\Models; use MongoDB\Laravel\Eloquent\Model; class Concert extends Model { protected $connection = 'mongodb'; protected $fillable = ['performer', 'venue', 'genres', 'ticketsSold', 'performanceDate']; protected $casts = ['performanceDate' => 'datetime']; }
Tip
The $fillable attribute lets you use Laravel mass assignment for insert operations. To learn more about mass assignment, see Customize Mass Assignment in the Eloquent Model Class documentation.
The $casts attribute instructs Laravel to convert attributes to common data types. To learn more, see Attribute Casting in the Laravel documentation.
Delete One Document
You can delete one document in the following ways:
Call the
$model->delete()method on an instance of the model.Chain methods to retrieve and delete an instance of a model by calling the
delete()method.Call the
Model::destroy($id)method on the model, passing it the_idvalue of the document to be deleted.
delete() Method
The following example shows how to delete a document by calling $model->delete() on an instance of the model:
$concert = Concert::first(); $concert->delete();
When the delete() method succeeds, the operation returns the number of documents deleted.
If the retrieve part of the call does not match any documents in the collection, the operation returns 0.
The following example shows how to chain calls to retrieve the first matching document and delete it:
Concert::where('venue', 'Carnegie Hall') ->limit(1) ->delete();
Note
The orderBy() call sorts the results by the _id field to guarantee a consistent sort order. To learn more about sorting in MongoDB, see the Natural order glossary entry in the Server manual.
When the delete() method succeeds, it returns the number of documents deleted.
If the where() method does not match any documents, the delete() method returns 0.
destroy() Method
The following example shows how to delete a document by passing the value of its _id value to the Model::destroy($id) method:
$id = 'MSG-0212252000'; Concert::destroy($id);
When the destroy() method succeeds, it returns the number of documents deleted.
If the _id value does not match any documents, the destroy() method returns 0.
Delete Multiple Documents
You can delete multiple documents in the following ways:
Call the
Model::destroy($ids)method, passing a list of the ids of the documents or model instances to be deleted.Chain methods to retrieve a Laravel collection object that references multiple objects and delete them by calling the
delete()method.
destroy() Method
The following example shows how to delete a document by passing an array of _id values, represented by $ids, to the destroy() method:
$ids = [3, 5, 7, 9]; Concert::destroy($ids);
Tip
The destroy() method performance suffers when passed large lists. For better performance, use Model::whereIn('id', $ids)->delete() instead.
When the destroy() method succeeds, it returns the number of documents deleted.
If the _id values do not match any documents, the destroy() method returns 0.
delete() Method
The following example shows how to chain calls to retrieve matching documents and delete them:
Concert::where('ticketsSold', '>', 7500) ->delete();
When the delete() method succeeds, it returns the number of documents deleted.
If the where() method does not match any documents, the delete() method returns 0.
Fully Runnable Examples
The following sections provide fully runnable code examples that demonstrate how to delete documents by using the Laravel Integration. These examples use the sample_mflix.movies collection from the Atlas sample datasets.
Tip
You can run the full examples from your own Laravel application or from the my-app application created in the Get Started tutorial. The examples use the Movie.php model class created in this tutorial to demonstrate operations on the movies MongoDB collection. To run the operation, you can copy the sample code to a controller endpoint in your Laravel application.
To view the expected output of the operation, you can add a web route to your application that calls the controller function and returns the result to a web interface.
Delete a Document
Select from the following Eloquent and Query Builder tabs to view examples for each corresponding query syntax:
This example performs the following actions:
Uses the
MovieEloquent model to represent themoviescollection in thesample_mflixdatabaseDeletes a document from the
moviescollection 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 thetitlefield 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
moviescollection by calling thetable()method from theDBfacadeDeletes a document from the
moviescollection 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 thetitlefield 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
Delete Multiple Documents
Select from the following Eloquent and Query Builder tabs to view examples for 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
Additional Information
To learn about the Laravel features available in the Laravel Integration that modify delete behavior, see the following sections:
Soft delete, which lets you mark documents as deleted instead of removing them from the database
Pruning, which lets you define conditions that qualify a document for automatic deletion
To learn how to insert documents into a MongoDB collection, see the Insert Documents guide.