# DeleteMultiple with ARX.SDK on MongoDB

## What a DeleteMultiple does

| Rule | On MongoDB |
|---|---|
| By many ids | One `deleteMany` with `$in`; returns how many went |
| Many entities | DeleteMultiple is all or nothing when it checks; the same entity or match values twice is ARX1001 |

## The samples

Every DeleteMultiple sample, in reading order. Each scenario shows the code (verified against MongoDB) and the mongosh query that checks what it did. `service` is the application's `ArxMongoDbService`; values marked "In scope" come from earlier in the sample. What the samples' code refers to beyond the packages (SampleCatalog.cs, `dbContext`) is in the sample application file, arx-mongodb-sample-application.md.

### Delete many records in one call

Basics 1 of DeleteMultiple.

Many documents in one call, in two scenarios: by the ids Create returned, and by a list of entities found by the declared key (the account number).

Good to know:

By ids, ARX sends one `deleteMany` with `$in`. By entities, the deletes run in one transaction (a replica set or a sharded cluster). Either way the call returns how many documents went: an id or a key no document has isn't an error.

Usings:

```csharp
using ARX.SDK.MongoDb.Services;
using ARX.SDK.Services;
```

Set up by the sample before its scenarios:

```csharp
var ids = new List<ArxRecordId>();
foreach (var number in new[] { "ACC-M902", "ACC-M903", "ACC-M904", "ACC-M905" })
{
    var account = new ArxEntity("account");
    account["name"] = "To delete " + number;
    account["account_number"] = number;
    ids.Add(await service.CreateAsync(account));
}
```

#### 1. By ids

Two ids in one call.

In scope: `service` (ArxMongoDbService), `ids` (IReadOnlyList<ArxRecordId>).

```csharp
int deleted = await service.DeleteMultipleAsync("account", ids);

Console.WriteLine($"{deleted} documents deleted");
```

Check it in the database:

```sql
db.account.countDocuments({ account_number: { $in: ["ACC-M902", "ACC-M903"] } })
```

Expect: 0.

#### 2. By the declared key

A list of account numbers, one of them unknown: the two found go.

In scope: `service` (ArxMongoDbService).

```csharp
var requests = new[] { "ACC-M904", "ACC-M905", "ACC-M999" }.Select(number =>
{
    var request = new ArxEntity("account");
    request["account_number"] = number;
    return request;
}).ToList();

int deleted = await service.DeleteMultipleAsync(requests, new ArxDeleteOptions { Match = ArxMatch.Key("account_number") });

Console.WriteLine($"{deleted} of {requests.Count} documents deleted");
```

Check it in the database:

```sql
db.account.countDocuments({ account_number: { $in: ["ACC-M904", "ACC-M905"] } })
```

Expect: 0.

