Amazon DynamoDB · Delete
Delete one record
The smallest delete, in two scenarios: by the id Create returned, and an id no item has.
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 product = new ArxEntity("product");
product["sku"] = "SKU-D301";
product["name"] = "Clearance item SKU-D301";
var id = await service.CreateAsync(product);1 By the id Create returned
The id CreateAsync returned for SKU-D301.
Already in scope: service (ArxDynamoDbService), id (ArxRecordId)
bool deleted = await service.DeleteAsync("product", id);
Console.WriteLine($"deleted: {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" = 'SKU-D301'Expect: no items.
2 An id no item has
SKU-D301 again: nothing to delete, so false, not an error.
Already in scope: service (ArxDynamoDbService)
bool deleted = await service.DeleteAsync("product", "SKU-D301");
Console.WriteLine($"deleted: {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" = 'SKU-D301'Expect: no items.
Good to know
DeleteAsync(entityName, id) returns true when an item went, false when none had the id: deleting twice isn't an error. Several ids in one call are DeleteMultiple's.
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.Delete.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_DeleteOneRecord().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.Delete.Basics;
/// <summary>
/// The smallest delete, in two scenarios: by the id Create returned, and an id no item has.
/// </summary>
/// <remarks>
/// <c>DeleteAsync(entityName, id)</c> returns true when an item went, false when none had the id: deleting twice
/// isn't an error. Several ids in one call are DeleteMultiple's.
/// </remarks>
public sealed class Basic1_DeleteOneRecord : ISample
{
public string Name => "delete-basics-1-delete-one-record";
public string Title => "Delete one record";
public async Task RunAsync(SampleContext dbContext)
{
var product = new ArxEntity("product");
product["sku"] = "SKU-D301";
product["name"] = "Clearance item SKU-D301";
var id = await dbContext.Service.CreateAsync(product);
await Scenario1_ByTheIdCreateReturnedAsync(dbContext.Service, id);
await Scenario2_AnIdNoItemHasAsync(dbContext.Service);
}
/// <summary>1. The id CreateAsync returned for SKU-D301.</summary>
private static async Task Scenario1_ByTheIdCreateReturnedAsync(ArxDynamoDbService service, ArxRecordId id)
{
Step("1. By the id Create returned");
bool deleted = await service.DeleteAsync("product", id);
Show($"deleted: {deleted}");
/* ===========================================================
* [DIRECT QUERY CHECK]
* -----------------------------------------------------------
* SELECT "sku" FROM "product" WHERE "sku" = 'SKU-D301'
* -- Expect: no items.
* ===========================================================
*/
}
/// <summary>2. SKU-D301 again: nothing to delete, so false, not an error.</summary>
private static async Task Scenario2_AnIdNoItemHasAsync(ArxDynamoDbService service)
{
Step("2. An id no item has");
bool deleted = await service.DeleteAsync("product", "SKU-D301");
Show($"deleted: {deleted}");
/* ===========================================================
* [DIRECT QUERY CHECK]
* -----------------------------------------------------------
* SELECT "sku" FROM "product" WHERE "sku" = 'SKU-D301'
* -- Expect: no items.
* ===========================================================
*/
}
}
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. By the id Create returned
deleted: True
> 2. An id no item has
deleted: False