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

Delete Documents

On this page

  • Overview
  • Sample Data
  • Delete Operations
  • Delete One Document
  • Delete Multiple Documents
  • Parameters
  • Example
  • Return Value
  • Additional Information
  • API Documentation

In this guide, you can learn how to remove documents from your MongoDB collections using delete operations.

The examples in this guide use the restaurants collection from the sample_restaurants database. The documents in this collection use the following Restaurant, Address, and GradeEntry classes as models:

public class Restaurant
{
public ObjectId Id { get; set; }
public string Name { get; set; }
[BsonElement("restaurant_id")]
public string RestaurantId { get; set; }
public string Cuisine { get; set; }
public Address Address { get; set; }
public string Borough { get; set; }
public List<GradeEntry> Grades { get; set; }
}
public class Address
{
public string Building { get; set; }
[BsonElement("coord")]
public double[] Coordinates { get; set; }
public string Street { get; set; }
[BsonElement("zipcode")]
public string ZipCode { get; set; }
}
public class GradeEntry
{
public DateTime Date { get; set; }
public string Grade { get; set; }
public float? Score { get; set; }
}

Note

The documents in the restaurants collection use the snake-case naming convention. The examples in this guide use a ConventionPack to deserialize the fields in the collection into Pascal case and map them to the properties in the Restaurant class.

To learn more about custom serialization, see Custom Serialization.

This collection is from the sample datasets provided by Atlas. See the Quick Start to learn how to create a free MongoDB cluster and load this sample data.

Use delete operations to remove documents that match a query filter. The query filter determines which records are selected for deletion based on the criteria in the query filter document. You can perform delete operations in MongoDB with the following methods:

  • DeleteOne(), which deletes the first document that matches the query filter

  • DeleteMany(), which deletes all documents that match the query filter

The following code shows how to use the asynchronous DeleteOneAsync() method or the synchronous DeleteOne() method to delete one document.

var result = await _restaurantsCollection.DeleteOneAsync(filter);
var result = _restaurantsCollection.DeleteOne(filter);

The following code shows how to use the asynchronous DeleteManyAsync() method or the synchronous DeleteMany() method to delete all matched documents.

var result = await _restaurantsCollection.DeleteManyAsync(filter);
var result = _restaurantsCollection.DeleteMany(filter);

Tip

Find runnable examples using these methods under additional information.

The DeleteOne() and DeleteMany() methods require you to pass a query filter specifying which documents to match. More information on how to construct a query filter is available in the Query Documents tutorial.

Both methods optionally take a DeleteOptions type as an additional parameter, which represents options you can use to configure the delete operation. If you don't specify any DeleteOptions properties, the driver does not customize the delete operation.

The DeleteOptions type allows you to configure options with the following properties:

Property
Description

Collation

Gets or sets the type of language collation to use when sorting results. See the delete statements for more information.

Comment

Gets or sets the comment for the operation. See the delete command fields for more information.

Hint

Gets or sets the index to use to scan for documents. See the delete statements for more information.

Let

Gets or sets the let document. See the delete command fields for more information.

The following code uses the DeleteMany() method to search on the "borough_1" index and delete all documents where the address.street field value includes the phrase "Pearl Street":

var filter = Builders<Restaurant>.Filter
.Regex("address.street", "Pearl Street");
DeleteOptions opts = new DeleteOptions { Hint = "borough_1" };
Console.WriteLine("Deleting documents...");
var result = _restaurantsCollection.DeleteMany(filter, opts);
Console.WriteLine($"Deleted documents: {result.DeletedCount}");
Console.WriteLine($"Result acknowledged? {result.IsAcknowledged}");
Deleting documents...
Deleted documents: 26
Result acknowledged? True

Tip

If the preceding example used the DeleteOne() method instead of DeleteMany(), the driver would delete the first of the 26 matched documents.

The DeleteOne() and DeleteMany() methods return a DeleteResult type. This type contains the DeletedCount property, which indicates the number of documents deleted, and the IsAcknowledged property, which indicates if the result is acknowledged. If the query filter does not match any documents, no documents are deleted and DeletedCount is 0.

For runnable examples of the delete operations, see the following usage examples:

To learn more about any of the methods or types discussed in this guide, see the following API Documentation:

Back

Replace