Soft Deletes using EF Core Interceptors

This post will teach us how to use EF Core Interceptors for Soft Deletes. Please read my previous essay on Entity Framework before we begin.

Now, let’s get started.

Soft delete using EF Core interceptors is an excellent pattern to automatically filter out “deleted” entities without physically removing them from the database. You mark entities as deleted by setting a flag (e.g., IsDeleted), and the interceptor ensures queries ignore those flagged records.

What is Soft Delete?

  • Instead of physically deleting a row from the database, you mark it as deleted (e.g., with a Boolean IsDeleted column or a DeletedAt timestamp).
  • This allows you to keep historical data and recover deleted records if needed.
  • You want your queries to exclude soft-deleted entities automatically.

Why Use EF Core Interceptors?

  • Interceptors let you hook into EF Core’s SaveChanges pipeline.
  • You can intercept delete operations and convert them to update operations that set IsDeleted = true.
  • You can globally apply query filters to exclude soft-deleted records.

Step-by-Step Example

1. Define a Soft Delete Interface

First, define an interface for soft-deletable entities.

Entities that support soft delete implement this.

2. Create a SaveChangesInterceptor for Soft Delete

You want to intercept delete operations and convert them into an update setting IsDeleted = true.

3. Register the Interceptor in DbContext

In your DbContext class, register the interceptor.

4. Global Query Filter for Soft Deleted Entities

Note: the global query filter.HasQueryFilter(p => !p.IsDeleted); which automatically excludes soft-deleted entities from queries.

Summary

  • Entities implement ISoftDelete with the IsDeleted property.
  • Interceptor converts deletes into updates that mark entities as soft deleted (IsDeleted = true).
  • Global query filters exclude soft-deleted entities from queries.
  • Your app “soft deletes” transparently.

Conclusion

In this article, I have tried to cover Soft Deletes using EF Core Interceptors.

ASP.NET 10.0 Hosting Recommendation

ASP.NET is a powerful platform for creating web applications and services. You must be comfortable with JavaScript, HTML, CSS, and C# before developing a web application in ASP.NET. On the market, there are thousands of web hosting companies providing ASP.NET Hosting. But, only very few web hosting companies could provide high quality ASP.NET hosting solution.

ASP.NET is the best development language in Windows platform, which is released by Microsoft and widely used to build all types of dynamic Web sites and XML Web services. With this article, we’re going to help you to find the best ASP.NET Hosting solution in Europe based on reliability, features, price, performance and technical support. After we reviewed about 30+ ASP.NET hosting providers in Europe, our Best ASP.NET Hosting Award in Europe goes to HostForLIFE.eu, one of the fastest growing private companies and one of the most reliable hosting providers in Europe.

You may also like...

Popular Posts

Skip to toolbar Log Out