How would you implement soft deletion without accidentally returning deleted data from Entity Framework Core queries?
BrowserStack · Full Stack Developer · 3-4-years · technical
Published
Year not specified · .NET, Entity Framework Core, Authentication & Security
Community answers
Posted by wasAsked editorial collection
Add a nullable DeletedAt (and optionally DeletedBy) and use an EF Core global query filter. Soft deletion should be an explicit domain operation, not an accidental update.
Example:
modelBuilder.Entity<Product>().HasQueryFilter(x =>
x.DeletedAt == null &&
x.RestaurantId == tenant.RestaurantId &&
x.OutletId == tenant.OutletId);
product.DeletedAt = clock.UtcNow;
Administrative restore queries may use IgnoreQueryFilters(), but must immediately reapply RestaurantId and OutletId from the authenticated context. Decide how unique indexes treat deleted rows, cascade or restrict dependent records deliberately, and retain hard-delete/retention procedures for privacy and storage requirements.
Backbencher Developer · · 0 likes
Soft deletion marks rows as deleted (IsDeleted or DeletedAt) instead of removing them. The risk is forgetting the filter somewhere and showing deleted data.
1. Global query filters
modelBuilder.Entity<Customer>().HasQueryFilter(c => !c.IsDeleted);
Every LINQ query on Customer, including Include and navigation loading, automatically adds WHERE IsDeleted = 0. Apply it to all soft-deletable entities via an interface and a loop in OnModelCreating.
2. Convert deletes centrally
Override SaveChanges or use a SaveChangesInterceptor:
foreach (var entry in ChangeTracker.Entries<ISoftDeletable>().Where(e => e.State == EntityState.Deleted))
{
entry.State = EntityState.Modified;
entry.Entity.IsDeleted = true;
entry.Entity.DeletedAt = DateTime.UtcNow;
entry.Entity.DeletedBy = _currentUser.Id;
}
Now Remove() behaves consistently everywhere.
3. Watch the gaps
- Raw SQL (FromSql, Dapper, stored procedures, views, reporting queries) bypasses EF filters — add the condition explicitly or expose filtered views.
- IgnoreQueryFilters() turns off every filter, including tenant filters if you use them for multi-tenancy. Restrict its use to admin or restore features, and review it in code review. EF Core 10 adds named query filters so one filter can be disabled without the others.
- Required navigations to a filtered entity can make parents disappear from results; EF Core warns about this. Decide whether related data should be filtered too.
- Unique indexes: a deleted row can block re-creating the same email or SKU. Use a filtered unique index (WHERE IsDeleted = 0).
- ExecuteDelete bypasses the change tracker, so it hard-deletes; use ExecuteUpdate to set the flag.
4. Performance and compliance
- Index the IsDeleted column or use filtered indexes for active rows.
- Soft deletion is not erasure: for GDPR or DPDP requests, really remove or anonymise personal data, and purge old soft-deleted rows on a schedule.
5. Test it
Integration tests that delete an entity and assert it does not appear in lists, searches, counts, includes or API detail endpoints.