Path: redb/redb.Examples/Examples/E182_DeleteBatch.cs
Snapshot: redb, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb
Page: https://redbase.app/src/dev-2026-12-06/redb/redb.Examples/Examples/E182_DeleteBatch.cs
----
using System.Diagnostics;
using redb.Core;
using redb.Core.Models.Entities;
using redb.Examples.Models;
using redb.Examples.Output;
namespace redb.Examples.Examples;
///
/// Demonstrates batch deletion of multiple objects.
/// Creates temporary objects, then deletes them all at once.
///
[ExampleMeta("E182", "Delete - Batch", "Core",
ExampleTier.Free, 182, "Delete", "Batch", "Multiple", RelatedApis = ["IRedbService.DeleteAsync"])]
public class E182_DeleteBatch : ExampleBase
{
public override async Task RunAsync(IRedbService redb)
{
var sw = Stopwatch.StartNew();
// Create temporary objects to delete
var scheme = (await redb.GetSchemeByTypeAsync())!;
var batchId = DateTime.UtcNow.Ticks;
var tempObjects = new List>();
for (int i = 0; i < 5; i++)
{
tempObjects.Add(new RedbObject
{
name = $"BatchDelete_{batchId}_{i}",
scheme_id = scheme.Id,
Props = new EmployeeProps
{
FirstName = $"Temp{i}",
LastName = "Employee",
Age = 20 + i,
Salary = 40000m + i * 1000,
Department = "Test"
}
});
}
// Save all
var ids = await redb.SaveAsync(tempObjects.Cast());
// Verify count before delete
var countBefore = (await redb.LoadAsync(ids)).Count;
// Delete all by IDs in one call
await redb.DeleteAsync(ids);
// Verify count after delete
var countAfter = (await redb.LoadAsync(ids)).Count;
sw.Stop();
return Ok("E182", "Delete - Batch", ExampleTier.Free, sw.ElapsedMilliseconds, ids.Count,
[$"Created: {ids.Count} objects",
$"Before delete: {countBefore}",
$"After delete: {countAfter}"]);
}
}