For AI agents: a documentation index is available at https://www.mongodb.com/docs/llms.txt — markdown versions of all pages are available by appending .md to any URL path.
Docs Menu

Delete Documents

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:

The operations in this guide reference the following Eloquent model class:

Concert.php
<?php
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.

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 _id value of the document to be deleted.

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.

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.

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.

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.

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.

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.

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 Movie Eloquent model to represent the movies collection in the sample_mflix database

  • Deletes a document from the movies collection that matches a query filter

  • Prints the number of deleted documents

The example calls the following methods on the Movie model:

  • where(): Matches documents in which the value of the title field is "Quiz Show"

  • limit(): Retrieves only the first matching document

  • delete(): 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 the table() method from the DB facade

  • Deletes a document from the movies collection that matches a query filter

  • Prints the number of deleted documents

The example calls the following query builder methods:

  • where(): Matches documents in which the value of the title field is "Quiz Show"

  • limit(): Retrieves only the first matching document

  • delete(): Deletes the retrieved document

$deleted = DB::table('movies')
->where('title', 'Quiz Show')
->limit(1)
->delete();
echo 'Deleted documents: ' . $deleted;
Deleted documents: 1

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 Movie Eloquent model to represent the movies collection in the sample_mflix database

  • Deletes documents from the movies collection that match a query filter

  • Prints the number of deleted documents

The example calls the following methods on the Movie model:

  • where(): Matches documents in which the value of the year field is less than or equal to 1910

  • delete(): 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 movies collection by calling the table() method from the DB facade

  • Deletes documents from the movies collection that match a query filter

  • Prints the number of deleted documents

The example calls the following query builder methods:

  • where(): Matches documents in which the value of the year field is less than or equal to 1910

  • delete(): 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 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.