Handling Very Large Deletes and Updates with EF

4/17/2026

During development, we typically work with small amounts of data. Let's say we need to delete several records. We're include to write Entity Framework code like this:

using (var context = new MyDbContext())
{
   var deleteList = context.LogEntries.Where(l => l.Timestamp < somedate).ToList();
   context.LogEntries.RemoveRange(deleteList);
   context.SaveChanges(); 
}

This works fine in Dev, but could later break in Prod with there are millions of records. The above code is highly inefficient, particularly with large data sets. It unnecessarily creates a list in memory.

A Better Delete

With EF Core 7, we have a much better approach: ExecuteDelete. It looks like this:

using (var context = new MyDbContext())
{
   await context.LogEntries
      .Where(l => l.Timestamp < somedate)
      .ExecuteDeleteAsync(); 
}

This executes immediately. No .SaveChanges() needed. And it's a one-way trip. Much more efficient.

Also Updates!

EF Core 7 also provides ExecuteUpdate. It looks like this:

using (var context = new MyDbContext())
{
   await context.Students
      .Where(a => a.GraduationDate != null)
      .ExecuteUpdateAsync(a =>
          a.SetProperty(s => s.IsActiveEnroll, false)
           .SetProperty(s => s.SomeInt, 0)); 
}

Notice this allows chaining of multiple SetProperty calls. These will all get compiled into a single UPDATE statement.

NOTE: But keep in mind this executes immediately, an not through a .SaveChanges() call. It's also restricted to updates on a single table.

But despite that, this is an excellent addition to EF.


Please register or login to add a comment.

Comments (displaying 1 - 1):
No comments yet! Be the first...


  • C#/.NET
  • T-SQL
  • HTML/CSS
  • JavaScript/jQuery
  • C++
  • .NET
  • ASP.NET/MVC
  • Xamarin/MAUI
  • WPF
  • Windows 11
  • SQL Server 20xx
  • Android
  • XBox
  • Arduino
  • Skiing
  • Rock Climbing
  • White water kayaking
  • Road Biking