# Soft Deletion Of Entities

When deleting an entity in Ucommerce using

```csharp
_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.

```csharp
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.

```csharp
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


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://dev.ucommerce.net/readme/miscellaneous/soft-deletion-of-entities.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
