Amazon DynamoDB · DeleteMultiple
Delete many records in one call
Many items in one call: three SKUs, one of them unknown. The two found go, in one transaction, and the call returns how many.
Set up for this page
The scenarios build on the sample application: SampleContext.cs (the dbContext every sample is given); SampleOutput.cs (Step and Show, which print); SampleContext.Application.cs (dbContext.Service, the application's ARX service); SampleModel.cs (the DynamoDB layout and the schema catalog); SampleContext.Direct.cs (dbContext.Direct); DynamoDirectHelper.cs (the checks behind dbContext.Direct).
dbContext.Direct is the samples' own checking code on the plain driver, not part of ARX: leave it out of your application.
Before scenario 1 the sample runs this, so the service and the rows the scenarios use are there:
var products = new[] { "SKU-D302", "SKU-D303", "SKU-D304" }.Select(sku =>
{
var product = new ArxEntity("product");
product["sku"] = sku;
product["name"] = $"Clearance item {sku}";
return product;
}).ToList();
await service.CreateMultipleAsync(products);1 Several ids at once
Three SKUs, one of them unknown: the two found go, in one transaction.
Already in scope: service (ArxDynamoDbService)
int deleted = await service.DeleteMultipleAsync("product", new[] { "SKU-D302", "SKU-D303", "SKU-D399" });
Console.WriteLine($"{deleted} items deleted");Check it in the database
A PartiQL statement: run it in NoSQL Workbench, or with aws dynamodb execute-statement --statement, after the code has run.
SELECT "sku" FROM "product" WHERE "sku" IN ['SKU-D302', 'SKU-D303', 'SKU-D304']Expect: 1 item, SKU-D304.
Good to know
The deletes are sent conditioned on each item, without a read first, as one TransactWriteItems of at most 100 items (more is ARX8001 unless AllowPartial). An id no item has isn't an error; it isn't counted. Delete's "How to find the items" sample deletes many by columns a key or an index gives.
Run the complete sample
The code above comes from one program, built in a fresh project and run against the database. To run it yourself, here is every file and step.
The program, its files and how to run it
Run it locally
- .NET SDK: .NET SDK 10.0.100 or later 10.0.x (target
net10.0). - Create the project:
Terminal dotnet new console -n ArxSample -f net10.0 cd ArxSample - Install the packages:
Terminal dotnet add package ARX.SDK.DynamoDb --version 1.0.0-rc.1 - Database: a Amazon DynamoDB, or DynamoDB Local for development (tested with DynamoDB Local 2.5.2) database with the sample schema.
- Configuration: set
ARX_SAMPLES_DYNAMODBto its connection string. Nothing is hard-coded. - Source code: add the files of the sample application this sample uses (
SampleContext.cs,SampleOutput.cs,SampleContext.Application.cs,SampleModel.cs,SampleContext.Direct.cs,DynamoDirectHelper.cs), then replaceProgram.csand add the other file below, with the file names shown. - Run it:
Terminal dotnet run - Expected output: below.
The files
Runs the sample. Generated for this page; it reads the connection string from the environment and first remakes the sample database from sample-schema.json.
using ArxSamples.DynamoDb;
using ArxSamples.DynamoDb.Operations.DeleteMultiple.Basics;
// The connection string comes from the environment, never from the code (see "Configure the connection").
var connectionString = Environment.GetEnvironmentVariable("ARX_SAMPLES_DYNAMODB")
?? throw new InvalidOperationException("Set the ARX_SAMPLES_DYNAMODB environment variable to your Amazon DynamoDB connection string.");
var dbContext = new SampleContext(connectionString);
// Every run starts from the sample data: this deletes and makes again what sample-schema.json lists
// (save the file in the project folder, see Get started).
await dbContext.Direct.ResetAsync();
await new Basic1_DeleteManyRecords().RunAsync(dbContext);
The sample, as it is in the repository.
using ARX.SDK.DynamoDb.Services;
using ARX.SDK.Services;
using static ArxSamples.DynamoDb.SampleOutput;
namespace ArxSamples.DynamoDb.Operations.DeleteMultiple.Basics;
/// <summary>
/// Many items in one call: three SKUs, one of them unknown. The two found go, in one transaction, and the call returns
/// how many.
/// </summary>
/// <remarks>
/// The deletes are sent conditioned on each item, without a read first, as one <c>TransactWriteItems</c> of at most 100
/// items (more is ARX8001 unless <c>AllowPartial</c>). An id no item has isn't an error; it isn't counted. Delete's
/// "How to find the items" sample deletes many by columns a key or an index gives.
/// </remarks>
public sealed class Basic1_DeleteManyRecords : ISample
{
public string Name => "delete-multiple-basics-1-delete-many-records";
public string Title => "Delete many records in one call";
public async Task RunAsync(SampleContext dbContext)
{
var products = new[] { "SKU-D302", "SKU-D303", "SKU-D304" }.Select(sku =>
{
var product = new ArxEntity("product");
product["sku"] = sku;
product["name"] = $"Clearance item {sku}";
return product;
}).ToList();
await dbContext.Service.CreateMultipleAsync(products);
await Scenario1_SeveralIdsAtOnceAsync(dbContext.Service);
}
/// <summary>1. Three SKUs, one of them unknown: the two found go, in one transaction.</summary>
private static async Task Scenario1_SeveralIdsAtOnceAsync(ArxDynamoDbService service)
{
Step("1. Several ids at once");
int deleted = await service.DeleteMultipleAsync("product", new[] { "SKU-D302", "SKU-D303", "SKU-D399" });
Show($"{deleted} items deleted");
/* ===========================================================
* [DIRECT QUERY CHECK]
* -----------------------------------------------------------
* SELECT "sku" FROM "product" WHERE "sku" IN ['SKU-D302', 'SKU-D303', 'SKU-D304']
* -- Expect: 1 item, SKU-D304.
* ===========================================================
*/
}
}
Expected output
What dotnet run printed when the sample was verified, on a freshly created sample schema. Ids differ when the tables already hold rows.
> 1. Several ids at once
2 items deleted