Entity Framework Core Interview Question #112

What is ExecuteDelete in EF Core?

Change Tracking, Updates, Transactions & Concurrency Mid-Level Intermediate

Quick Interview Answer

ExecuteDelete performs a set-based DELETE directly in the database for rows matching a LINQ query. The entities do not need to be loaded or tracked first.

Detailed Explanation

ExecuteDelete performs a set-based DELETE directly in the database for rows matching a LINQ query. The entities do not need to be loaded or tracked first.

This can be much more efficient than loading many entities and calling Remove for each one.

Like ExecuteUpdate, it bypasses tracked-entity SaveChanges behavior, so consider relationships, database constraints, auditing requirements, and stale tracked instances.

Code Example

await db.AuditLogs
    .Where(x => x.CreatedAtUtc < cutoff)
    .ExecuteDeleteAsync();