Content · Programming · PHP

PHP MongoDB\Driver\BulkWrite::delete() Method: Usage and Examples

Adds a document deletion operation with a filter to an ordered or unordered MongoDB bulk write. Delete filters must be intentional; use limit 1 for a single match and 0 for all matches.

2 views Published Oct 17, 2026 Updated Oct 17, 2026 Know-Stuff

Adds a document deletion operation with a filter to an ordered or unordered MongoDB bulk write.

The examples focus on the operation itself, including the important distinction: insert adds a document; delete queues deletion of matching documents.

What It Does


Use the examples below to observe the behavior on a small dataset before extending it to application code.

Syntax and Return Value


MongoDB\Driver\BulkWrite::delete(array|object $filter, ?array $deleteOptions = null): void

The displayed signature describes the API or identifies any correction or optional dependency. When a parameter is optional, supply it only when the task requires it.

Example 1: Basic Use


Requires PECL mongodb and a reachable server. BulkWrite queues changes locally; only executeBulkWrite sends them. Cursors stream server results.

<?php
$manager = new MongoDB\Driver\Manager(getenv("MONGODB_URI") ?: "mongodb://127.0.0.1:27017");
$bulk = new MongoDB\Driver\BulkWrite();
$bulk->delete(["task" => 'ship'], ["limit" => 1]);
$result = $manager->executeBulkWrite('demo.tasks', $bulk);
echo $result->isAcknowledged() ? "acknowledged" : "unacknowledged";

This example is meant for a configured application environment; output depends on the installed extension, external service, or database contents.

Example 2: Checking Behavior


Requires PECL mongodb and a reachable server. BulkWrite queues changes locally; only executeBulkWrite sends them. Cursors stream server results.

<?php
$manager = new MongoDB\Driver\Manager(getenv("MONGODB_URI") ?: "mongodb://127.0.0.1:27017");
$bulk = new MongoDB\Driver\BulkWrite();
$bulk->delete(["task" => 'review'], ["limit" => 1]);
$result = $manager->executeBulkWrite('demo.audit', $bulk);
echo $result->isAcknowledged() ? "acknowledged" : "unacknowledged";

This example is meant for a configured application environment; output depends on the installed extension, external service, or database contents.

Example 3: Practical Example


Requires PECL mongodb and a reachable server. BulkWrite queues changes locally; only executeBulkWrite sends them. Cursors stream server results.

<?php
$manager = new MongoDB\Driver\Manager(getenv("MONGODB_URI") ?: "mongodb://127.0.0.1:27017");
$bulk = new MongoDB\Driver\BulkWrite();
$bulk->delete(["task" => 'archive'], ["limit" => 1]);
$result = $manager->executeBulkWrite('demo.queue', $bulk);
echo $result->isAcknowledged() ? "acknowledged" : "unacknowledged";

This example is meant for a configured application environment; output depends on the installed extension, external service, or database contents.

Important Behavior and Edge Cases


Delete filters must be intentional; use limit 1 for a single match and 0 for all matches.

This API is supplied by an optional extension that is not installed in the local test runtime, or requires a different PHP version. The example must be verified in a matching environment.

Comparison with Related Approaches


ApproachDifference
MongoDB\Driver\BulkWrite::deleteAdds a document deletion operation with a filter to an ordered or unordered MongoDB bulk write.
Related API or alternativeinsert adds a document; delete queues deletion of matching documents.

Common Beginner Mistakes


MistakeCorrect approach
Ignoring the operation-specific limitsDelete filters must be intentional; use limit 1 for a single match and 0 for all matches.
Confusing it with a related APIinsert adds a document; delete queues deletion of matching documents.

Key Takeaways


  • Adds a document deletion operation with a filter to an ordered or unordered MongoDB bulk write.
  • Delete filters must be intentional; use limit 1 for a single match and 0 for all matches.
  • insert adds a document; delete queues deletion of matching documents.

Use MongoDB\Driver\BulkWrite::delete when its documented behavior matches the problem. Test the relevant edge cases in your own runtime before deploying it.

Share this resourceSend this page to someone who may find it useful.