|
| 1 | +.. _laravel-delete-many-usage: |
| 2 | + |
| 3 | +========================= |
| 4 | +Delete Multiple Documents |
| 5 | +========================= |
| 6 | + |
| 7 | +.. facet:: |
| 8 | + :name: genre |
| 9 | + :values: reference |
| 10 | + |
| 11 | +.. meta:: |
| 12 | + :keywords: delete many, remove, code example |
| 13 | + |
| 14 | +.. contents:: On this page |
| 15 | + :local: |
| 16 | + :backlinks: none |
| 17 | + :depth: 1 |
| 18 | + :class: singlecol |
| 19 | + |
| 20 | +You can delete multiple documents in a collection by calling the ``delete()`` method on an |
| 21 | +object collection or a query builder. |
| 22 | + |
| 23 | +To delete multiple documents, pass a query filter to the ``where()`` method. Then, delete the |
| 24 | +matching documents by calling the ``delete()`` method. |
| 25 | + |
| 26 | +Example |
| 27 | +------- |
| 28 | + |
| 29 | +This usage example performs the following actions: |
| 30 | + |
| 31 | +- Uses the ``Movie`` Eloquent model to represent the ``movies`` collection in the |
| 32 | + ``sample_mflix`` database |
| 33 | +- Deletes documents from the ``movies`` collection that match a query filter |
| 34 | +- Prints the number of deleted documents |
| 35 | + |
| 36 | +The example calls the following methods on the ``Movie`` model: |
| 37 | + |
| 38 | +- ``where()``: matches documents in which the value of the ``year`` field is less than or |
| 39 | + equal to ``1910``. |
| 40 | +- ``delete()``: deletes the retrieved documents. This method returns the number of documents |
| 41 | + that were successfully deleted. |
| 42 | + |
| 43 | +.. io-code-block:: |
| 44 | + :copyable: true |
| 45 | + |
| 46 | + .. input:: ../includes/usage-examples/DeleteManyTest.php |
| 47 | + :start-after: begin-delete-many |
| 48 | + :end-before: end-delete-many |
| 49 | + :language: php |
| 50 | + :dedent: |
| 51 | + |
| 52 | + .. output:: |
| 53 | + :language: console |
| 54 | + :visible: false |
| 55 | + |
| 56 | + Deleted documents: 7 |
| 57 | + |
| 58 | +To learn how to edit your Laravel application to run the usage example, see the |
| 59 | +:ref:`Usage Examples landing page <laravel-usage-examples>`. |
| 60 | + |
| 61 | +.. tip:: |
| 62 | + |
| 63 | + To learn more about deleting documents with {+odm-short+}, see the :ref:`laravel-fundamentals-delete-documents` |
| 64 | + section of the Write Operations guide. |
| 65 | + |
| 66 | + For more information about query filters, see the :ref:`laravel-retrieve-matching` section of |
| 67 | + the Read Operations guide. |
| 68 | + |
0 commit comments