If ?retrywrites=true, DeleteManyAsync() returns a MongoWriteException

using MongoDB C# Driver, and trying to use DeleteManyAsync() with retryable writes, so I configured it, and included ?retrywrites=true in the connection string.

Tried using DeleteManyAsync() and getting this error

```A write operation resulted in an error. WriteError: { Category : “Uncategorized”, Code : 72, Message : “Cannot use (or request) retryable writes with limit=0” }.`

So I set ?retrywrites=false, and ``DeleteManyAsync()` started working.

I saw in the documentation for retryable writes that DeleteMany isn’t supported, only single document operations are - but I would expect DeleteManyAsync to just not retry and go through, instead of erroring when used. Any ideas?

The backend storage is Azure Cosmos DB for MongoDB, so maybe it could be either a Cosmos DB or a MongoDB C# driver bug/issue, I’m not too sure.

Hi @Armies_Wagons,

Welcome to the MongoDB Community Forums! That does not sound right. Both deleteMany and deleteManyAsync still go through even if retryWrites=true is set. This does not look like a MongoDB issue. Hope that helps.

Thanks,

Rishit.

Hi @Rishit_Bhatia
I am using deleteMany() in the code with retrywrites=false . I get the below
Exception Message: ‘A write operation resulted in an error. WriteError: { Category : “Uncategorized”, Code : 72, Message : “Cannot use (or request) retryable writes with limit=0” }.’

@Maithili_Kalkar the error you described does appear to come from the MongoDB server (mongo/src/mongo/db/ops/write_ops_exec.cpp at 00da7f442804d6118cc4bbc57caf9862e9811e2b · mongodb/mongo · GitHub), so it’s possible there may be an issue here.

Do you have a self-contained reproduction you could share that shows this failure in action?

Hi @alexbevi
I am using deleteMany to delete the documents present. I am not sure what do you exactly need.
Here is the line of code - collection.DeleteMany(filter_id);

@Maithili_Kalkar can you just confirm the version of the C# driver you’re using as well as the version of MongoDB you’re connecting to?

Also how is filter_id defined?

@alexbevi The C# driver is 2.23.1 and MongoDb Verison is MongoDB 3.6.0 Community

filter_id its a bson document containing key and value of document to be deleted

Hi @Maithili_Kalkar ,

Unfortunately, I’m not able to reproduce this error. I’m using MongoDB Community 3.6.0 with the C# driver 2.23.1 as you mentioned. The retryWrites option is set in my connection string as follows:

private string connectionString = "mongodb://localhost:27017/retryWrites=false";

The query used to perform the deleteManyAsync operation looks like this:

  var filter = Builders<Restaurant>.Filter.Eq(r => r.Cuisine, "Brazilian");
  var result = await _restaurantsCollection.Find(filter).ToListAsync();
  Console.WriteLine("Trying to Delete " + result.Count + " Restaurants");
  return await _restaurantsCollection.DeleteManyAsync(filter);

Not sure if that helps but if there’s any more information you can share or a sample zip file containing sample code to reproduce this, we’d be happy to take a look at it.

Thanks,

Rishit.

@Rishit_Bhatia @alexbevi

I found the issue for me, it was a cosmosdb thing. You can only use DeleteMany with retryable writes enabled, as long as the filter has the partition key.

I think the error message from MongoDb driver is a little misleading then, or at least, not clear what the issue is.

@Maithili_Kalkar I can only tag 2 people per post for the last post. see if my comment applies to you.

Thanks for confirming @Armies_Wagons.

Since the MongoDB driver wasn’t designed with CosmosDB in mind the failure conditions within that environment would be unpredictable, and may surface in (potentially confusing) ways such as this.

1 Like