Soft Deletion Of Entities

When deleting an entity in Ucommerce using

_dbContext.Remove(entity)
_dbContext.SavechangesAsync(cancellationToken)

or by using one of our APIs to remove an entity, EF Core deletion will be intercepted, and the entity will be checked for soft deletion. If the entity is soft deletable, the deleted flag will be set to true; otherwise, the deletion will go through to the database.

When an entity is soft deleted, its Deleted property will be set to true.

Permanently deleting an entity

It is possible to permanently delete soft deletable entities from the database. This can be done by calling ExecuteDelete or ExecuteDeleteAsync on the DbSet you want to delete from.

await _dbContext.Set<PriceGroup>()
    .Where(x => objectsToDelete.Select(p => p.Guid)
        .Contains(x.Guid))
    .ExecuteDeleteAsync(cancellationToken);

Remember to carefully filter your query if you wish to do this, as it circumvents all safety measures besides the ones directly built into the database.

Querying entities

When an entity is soft deleted, it will still be returned from direct queries to a DbContext. They can be filtered out based on the Deleted property.

await _dbContext.Set<PriceGroup>()
    .Where(x => !x.Deleted)
    .FirstOrDefaultAsync(x => x.Guid == guid, token)

List of soft deletable entities

The following entities are soft deleted by the system:

  • Campaign

  • Promotion

  • Category

  • Country

  • Currency

  • DataType

  • DataTypeEnum

  • Definition

  • DefinitionField

  • DefinitionType

  • EmailProfile

  • EmailType

  • OrderNumberSerie

  • PaymentMethod

  • PriceGroup

  • Catalog

  • Store

  • ProductDefinition

  • ProductDefinitionField

  • ShippingMethod

Last updated