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

Delete a Document

On this page

  • Example
  • Delete a Document by Using Builders
  • Expected Result
  • Additional Information
  • API Documentation

You can delete a document from a collection by using the synchronous DeleteOne() method, or the asynchronous DeleteOneAsync() method.

Note

The DeleteOne() method deletes only the first document that matches the filter. To delete more than one document, use the DeleteMany() method.

To learn more about using DeleteMany(), see Delete Many Documents.

The following example uses Builders to delete a document in the restaurants collection with the name "Ready Penny Inn".

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

var filter = Builders<Restaurant>.Filter
.Eq(r => r.Name, "Ready Penny Inn");
return await _restaurantsCollection.DeleteOneAsync(filter);

For a fully runnable example of the DeleteOne() method, see the Asynchronous Delete One Example.

var filter = Builders<Restaurant>.Filter
.Eq(r => r.Name, "Ready Penny Inn");
return _restaurantsCollection.DeleteOne(filter);

For a fully runnable example of the DeleteOne() method, see the Synchronous Delete One Example

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

Deleting a document with builders...
Deleted documents: 1

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

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

Back

Replace a Document