liveBook · Manning

Soft deletion

As explained in ASP.NET Core in Action, 3e

Soft delete is a deletion technique that marks a record as deleted so it is excluded from application exposure while remaining stored.

Soft deletion marks a record as deleted instead of removing it. Application queries exclude marked records, while the data remains stored. This is often safer and easier than physically deleting rows, although it does not suit every scenario. section 12.6 section 12.4.4

Implement the pattern by:

  • Adding an IsDeleted Boolean property to the entity. section 12.4.4
  • Setting IsDeleted to true in the delete operation, then saving the entity with SaveChangesAsync(). section 12.4.4
  • Adding Where clauses to retrieval methods so deleted records cannot be returned through the application. section 12.4.4

Marking a recipe as deleted

public async Task DeleteRecipe(int recipeId)\n{\n var recipe = await _context.Recipes.FindAsync(recipeId);\n if (recipe is null)\n throw new Exception("Unable to find the recipe");\n\n recipe.IsDeleted = true;\n await _context.SaveChangesAsync();\n} section 12.4.4

Listing 12.10 Marking entities as deleted in EF Core
public async Task DeleteRecipe(int recipeId)
{
    var recipe = await _context.Recipes.FindAsync(recipeId);  #1
    if(recipe is null) {                                      #2
        throw new Exception("Unable to find the recipe");     #2
    }                                                         #2
    recipe.IsDeleted = true;                                  #3
    await _context.SaveChangesAsync();                        #4
}

Shows the complete EF Core operation for locating an entity, marking it deleted, and saving the change. section 12.4.4

How ASP.NET Core in Action, 3e covers it

How deep it goes
Explains the flag, delete method, and retrieval filters, while deferring broader scenario-specific limitations.
Examples
Uses a Recipe entity, an IsDeleted property, DeleteRecipe, and Where clauses as working examples.
What it assumes
Assumes basic EF Core entity querying and saving, while demonstrating the deletion workflow explicitly.
What you can do after
Operate/configure an EF Core service that marks records deleted and prevents ordinary queries from returning them.
How it connects
Connects soft deletion with Entity Framework Core, LINQ, DbContext, and global query filters.

Also explained in

Compare how 2 books explain soft deletion →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of ASP.NET Core in Action, 3e. The section links point to the parts of the book it draws on.