Entity Framework Extensions Bulk Delete

Description

The EF BulkDelete extension method let you delete a large number of entities in your database.

// Easy to use
context.BulkDelete(customers);

// Easy to customize
context.BulkDelete(customers, options => options.BatchSize = 100);

Try it

Performance Comparison

Operations 1,000 Entities 2,000 Entities 5,000 Entities
SaveChanges 1,200 ms 2,400 ms 6,000 ms
BulkDelete 50 ms 55 ms 75 ms

Try it

HINT:

A lot of factors might affect the benchmark time such as index, column type, latency, throttling, etc.

Scenarios

The BulkDelete method is fast but also flexible to let you handle various scenarios in Entity Framework such as:

What is supported?

  • All Entity Framework versions (EF4, EF5, EF6, EF Core, EF Classic)
  • All Inheritances (TPC, TPH, TPT)
  • Complex Type/Owned Entity Type
  • Enum
  • Value Converter (EF Core)
  • And more!

Advantages

  • Easy to use
  • Flexible
  • Increase performance
  • Increase application responsiveness
  • Reduce database load
  • Reduce database round-trips

Getting Started

Bulk Delete

The BulkDelete and BulkDeleteAync methods extend your DbContext to let you delete a large number of entities in your database.

context.BulkDelete(customers);

context.BulkDeleteAsync(customers, cancellationToken);

Try it

Bulk Delete with options

The options parameter let you use a lambda expression to customize the way entities are deleted.

context.BulkDelete(customers, options => options.BatchSize = 100);

Try it

Why BulkDelete is faster than SaveChanges?

Deleting thousands of entities for a file importation is a typical scenario.

The SaveChanges method makes it quite impossible to handle this kind of situation due to the number of database round-trips required. The SaveChanges perform one database round-trip for every entity to delete. So if you need to delete 10,000 entities, 10,000 database round-trips will be performed which is INSANELY slow.

The BulkDelete in counterpart requires the minimum database round-trips as possible. By example under the hood for SQL Server, a SqlBulkCopy is performed first in a temporary table, then a DELETE from the temporary table to the destination table is performed which is the fastest way available.

Real Life Scenarios

Delete with custom key

You want to delete entities, but you don't have the primary key. The ColumnPrimaryKeyExpression let you use as a key any property or combination of properties.

context.BulkDelete(customers, options => options.ColumnPrimaryKeyExpression = c => c.Code);

Try it

Delete with future action

You want to delete entities, but you want to defer the execution.

By default, BulkDelete is an immediate operation. That mean, it's executed as soon as you call the method.

FutureAction: This option let you defer the execution of a Bulk Delete. ExecuteFutureAction: This option trigger and execute all pending FutureAction.

context.FutureAction(x => x.BulkDelete(customers1));
context.FutureAction(x => x.BulkDelete(customers2));

// ...code...

context.ExecuteFutureAction();

Try it

More scenarios

Hundred of scenarios has been solved and are now supported.

The best way to ask for a special request or to find out if a solution for your scenario already exists is by contacting us: info@zzzprojects.com

Documentation

BulkDelete

Methods
Name Description Example
BulkDelete<T>(items) Bulk delete entities in your database. Try it
BulkDelete<T>(items, options) Bulk delete entities in your database. Try it
BulkDeleteAsync<T>(items) Bulk delete entities asynchronously in your database.
BulkDeleteAsync<T>(items, cancellationToken) Bulk delete entities asynchronously in your database.
BulkDeleteAsync<T>(items, options, cancellationToken) Bulk delete entities asynchronously in your database.
Options

More options can be found here:

In this Page