BulkDeleteByKey

This method deletes rows from the database using a list of primary keys in bulk. It is supported only for Aurora MySQL.

This method deletes rows from the database using a list of primary keys in bulk. It is supported only for Aurora MySQL.

Call Flow Diagram

The diagram below shows the flow when calling this operation.

flowchart TD
    Client["Client<br/>(RepoDB)"] -->|BulkDeleteByKey| Keys["Primary Keys<br/>IEnumerable&lt;TPrimaryKey&gt;"]
    Keys --> Pseudo["Create Pseudo Table<br/>(key column only) + index"]
    Pseudo --> Copy["AuroraDbBulkCopy<br/>(LOAD DATA LOCAL)"]
    Copy -->|Write| PseudoTable[("Pseudo Table")]
    PseudoTable -->|"DELETE T FROM Target T<br/>INNER JOIN Pseudo S<br/>ON (key match)"| Table[("Target Table")]
    PseudoTable -->|Drop| Cleanup(["Pseudo Table Dropped"])

Use Case

Use this method to delete rows by primary key at high speed. It leverages AuroraDbBulkCopy, a LOAD DATA LOCAL INFILE-based bulk writer from RepoDb.Connector.AuroraDb.MySqlConnector.

Special Arguments

The bulkCopyTimeout, batchSize and pseudoTableType arguments are available for this operation.

batchSize overrides the number of rows sent to the server per batch. When not set, all items are sent at once.

pseudoTableType (via AuroraDbBulkImportPseudoTableType) is accepted but currently has no effect — every staged call resolves to Physical regardless of the value passed.

Usability

Pass the target table name and the list of primary keys to the operation.

using (var connection = new AuroraDbConnection(connectionString))
{
    var primaryKeys = connection.Query<Person>(p => p.IsActive == false).Select(p => p.Id);
    var deletedRows = connection.BulkDeleteByKey("Person", primaryKeys);
}

It returns the number of rows deleted from the underlying table.

To specify a batch size:

using (var connection = new AuroraDbConnection(connectionString))
{
    var primaryKeys = connection.Query<Person>(p => p.IsActive == false).Select(p => p.Id);
    var deletedRows = connection.BulkDeleteByKey("Person",
        primaryKeys,
        batchSize: 100);
}

Async Method

An equivalent BulkDeleteByKeyAsync method is also available.

using (var connection = new AuroraDbConnection(connectionString))
{
    var primaryKeys = connection.Query<Person>(p => p.IsActive == false).Select(p => p.Id);
    var deletedRows = await connection.BulkDeleteByKeyAsync("Person", primaryKeys);
}