Docs Menu
Docs Home
/ / /
Laravel MongoDB
/ /

Delete Documents

On this page

  • Overview
  • Sample Model
  • Delete One Document
  • delete() Method
  • destroy() Method
  • Delete Multiple Documents
  • destroy() Method
  • delete() Method
  • Additional Information

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:

  • Delete one document

  • Delete multiple documents

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.

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 view runnable code examples that demonstrate how to delete documents by using the Laravel Integration, see the following usage examples:

To learn how to insert documents into a MongoDB collection, see the Insert Documents guide.

Back

Modify