EF Core provides the Remove
and RemoveRange
methods for deleting entities.
However, to use them, you first need to retrieve those entities from the DbContext
, which takes time β and often doesnβt make sense.
Why would you retrieve something you just want to delete anyway?
The fastest and easiest way to delete multiple entities is by using the BulkDelete method from Entity Framework Extensions.
This third-party library for EF Core, created by ZZZ Projects, is extremely fast and offers hundreds of options β including all other bulk operations β to handle both common and complex scenarios.
In this example, weβll create a customer list, only specify the ID
, and call the delete method:
- without options
- with options
- and asynchronously
// @nuget: Z.EntityFramework.Extensions.EFCore
using Z.EntityFramework.Extensions;
var customersToDelete = new List<Customer>
{
new Customer { ID = 1 },
new Customer { ID = 13 }
// Add more customers as needed
};
using (var context = new YourDbContext())
{
// Easy to use
context.BulkDelete(customersToDelete);
// Easy to customize
context.BulkDelete(customersToDelete, options => options.BatchSize = 100);
}
using (var context = new YourDbContext())
{
// Supports async
await context.BulkDeleteAsync(customersToDelete);
}
π Why Use the Bulk Delete Method from Entity Framework Extensions?
The bulk delete method from Entity Framework Extensions is the recommended solution for EF Core when you need to delete a large number of entities very fast.
Entity Framework Extensions is the most popular and most complete library available, with over 50 million downloads and more than 5,000 paid customers. Bulk deleting in EF Core with Entity Framework Extensions is up to 3x faster, reducing execution time by 65% (Online Benchmark).
It also comes with several other advantages:
- β No need to load entities β Avoid change tracking and delete directly from the database.
- β Delete the way you want β Use custom keys, delete related entities (graph), or target specific conditions.
- β Extremely fast β Delete thousands or even millions of rows in just seconds. See the Performance Comparison.
- β Flexible with hundreds of options β Choose how relationships are handled, how keys are matched, and much more. Explore the Bulk Delete Options.
- β Supports all major providers β SQL Server, MySQL, MariaDB, Oracle, PostgreSQL, SQLite.
- β Most advanced EF Core library β Supports inheritance, owned types, complex types, and more.
π οΈ Commonly Used Options with the Bulk Delete Method from Entity Framework Extensions
Here are some of the most frequently used options with the bulk delete method from the Entity Framework Extensions library:
- ColumnPrimaryKeyExpression β Choose which properties should be used as the key by using a lambda expression. Only rows that match this key will be deleted.
- DeletePrimaryKeyAndFormula β Specify a hardcoded SQL formula to include additional logicβalong with the primary keyβto determine if the entity matches a row in the database. Only rows that also match this formula will be deleted.
- Batch β Customize
BatchSize
,BatchTimeout
, andBatchDelayInterval
to improve performance and control how deleted entities are grouped and executed. - IncludeGraph β Set to
true
if you want to delete both the main entities and their related entities.
For example, if you pass a list ofOrder
that includesOrderItem
, both will be deleted.
β οΈ Be careful: if you want to apply specific options to related entity types, youβll need to configure them usingIncludeGraphBuilder
. - RowsAffected β Set
UseRowsAffected = true
to access the number of deleted entities throughResultInfo.RowsAffected
orResultInfo.RowsAffectedDeleted
.
Learn more
π¦ How to Install Entity Framework Extensions
To use the bulk delete method, install the Z.EntityFramework.Extensions.EFCore NuGet package:
PM> NuGet\Install-Package Z.EntityFramework.Extensions.EFCore
> dotnet add package Z.EntityFramework.Extensions.EFCore
Entity Framework Extensions uses EF Core pinned versioning, so make sure the first digit of the package version matches your EF Core version. For example:
- If you're using
EF Core 9
, use versionv9.x.y.z
- If you're using
EF Core 8
, use versionv8.x.y.z
- And so on...
Looking for a different version, like BulkDelete
for EF6?
π Download it here
β Final Thoughts
When performance matters and you need to delete entities in bulkβwithout the overhead of tracking, loading, or writing raw SQLβthe bulk delete method from Entity Framework Extensions is the way to go.
Itβs fast, easy to use, and flexible enough to handle even the most advanced EF Core scenarios.
Whether you're cleaning up old data, syncing large datasets, or optimizing background jobs.
Give it a try and start deleting smarter, not slower.
π External Links
Looking for more resources? Here are some helpful links to learn more about the BulkDelete
method and Entity Framework Extensions, explore its documentation, and download the right version for your project.