Entity Framework Core Interview Question #164

What is soft delete and how can EF Core implement it?

Advanced Mapping, Interceptors, Value Conversions & Architecture Senior Advanced

Quick Interview Answer

Soft delete marks a row as deleted instead of physically removing it. A property such as IsDeleted or DeletedAtUtc records the state, while a global query filter can hide deleted rows from ordinary queries. The design must address uniqueness, related entities, auditing, retention, and intentional restoration or permanent deletion.

Detailed Explanation

Soft delete marks a row as deleted instead of physically removing it. A property such as IsDeleted or DeletedAtUtc records the state, while a global query filter can hide deleted rows from ordinary queries. The design must address uniqueness, related entities, auditing, retention, and intentional restoration or permanent deletion.

Code Example

product.IsDeleted = true;
product.DeletedAtUtc = DateTime.UtcNow;
await db.SaveChangesAsync();