If you’ve been using Entity Framework Core for a while, you know that sometimes you just need to delete or update a bunch of rows quickly without the overhead of pulling them into memory first. That’s where two newer methods—ExecuteDeleteAsync and ExecuteUpdateAsync—come in handy.
Instead of looping through entities and calling SaveChanges, these methods translate directly into SQL DELETE and UPDATE statements under the hood. That means fewer round trips and better performance.
Example: Bulk Deleting with ExecuteDeleteAsync
Let’s say you’re cleaning up old orders older than 2020. With the traditional way, you’d query them, remove them one by one, and save changes. But with ExecuteDeleteAsync, you can do it in a single database call:
await context.Orders
.Where(o => o.OrderDate < new DateTime(2020, 1, 1))
.ExecuteDeleteAsync();
Boom—EF Core sends a DELETE FROM Orders WHERE OrderDate < '2020-01-01' directly to the database. No objects tracked, no wasted memory.
Example: Bulk Updating with ExecuteUpdateAsync
Maybe you want to give all inactive users a new status flag. Normally, you’d loop through users, update the property, and save. Now you can do it inline:
await context.Users
.Where(u => !u.IsActive)
.ExecuteUpdateAsync(s => s
.SetProperty(u => u.Status, u => "Inactive")
.SetProperty(u => u.UpdatedAt, u => DateTime.UtcNow));
This generates a single SQL UPDATE statement and applies the changes instantly.
Why This Matters
- Performance: Great for bulk operations.
- Simplicity: Cleaner, more expressive code.
- Less Overhead: EF Core doesn’t have to materialize entities.
If you’re already on EF Core 7 or newer, these methods are ready to make your data cleanup and maintenance code a whole lot easier.
