Docs Menu
Docs Home
/ / /
C#/.NET
/

Delete Many Documents

On this page

  • Example
  • Expected Result
  • Additional Information
  • API Documentation

You can delete more than one document using the DeleteMany() synchronous method or the DeleteManyAsync() asynchronous method on a collection object.

The following code deletes all documents in the restaurants collection whose borough field value equals the word "Brooklyn".

Select the Asynchronous or Synchronous tab to see the corresponding code.

// Creates a filter for all documents that have a
// "borough" value of "Brooklyn"
var filter = Builders<Restaurant>.Filter
.Eq(r => r.Borough, "Brooklyn");
// Asynchronously deletes all documents that match the filter
return await _restaurantsCollection.DeleteManyAsync(filter);

For a fully runnable example of the DeleteManyAsync() operation, see the DeleteManyAsync code sample.

// Creates a filter for all documents that have a
// "borough" value of "Brooklyn"
var filter = Builders<Restaurant>.Filter
.Eq(r => r.Borough, "Brooklyn");
// Deletes all documents that match the filter
return _restaurantsCollection.DeleteMany(filter);

For a fully runnable example of the DeleteMany() operation, see the DeleteMany code sample.

Running either of the preceding full examples prints the following results:

Deleting documents...
Deleted documents: 6086
Resetting sample data...done.

To learn more about deleting documents, see the Delete Documents guide.

To learn more about using builders, see Operations with Builders.

Back

Delete a Document