Data Retention
Configurable data retention system for ASP.NET Core applications. Automatically manages soft-deleted records based on retention policies configured in appsettings.json.
Features
- ✅ Multiple Strategies - Choose between delete, archive, or implement custom strategies.
- ✅ Configurable Retention Policies - Define different retention periods for each table.
- ✅ Background Service - Runs automatically on a configurable schedule.
- ✅ Table-Specific Settings - Configure column names and retention days per table.
- ✅ Enable/Disable - Toggle cleanup globally or per table.
- ✅ Extensible - Implement
IDataRetentionStrategyfor custom behavior.
Installation
Delete Strategy (Permanent Deletion)
dotnet add package MasLazu.AspNet.DataRetention.Database.Delete
Usage
1. Register the Service
In your Program.cs:
using MasLazu.AspNet.DataRetention.Database.Delete.Extensions;
var builder = WebApplication.CreateBuilder(args);
// Add database data retention with delete strategy
builder.Services.AddDatabaseDataRetentionWithDelete(builder.Configuration);
2. Configure in appsettings.json
{
"DataRetention": {
"Enabled": true,
"CleanupIntervalHours": 24,
"InitialDelayMinutes": 10,
"Schema": "public",
"Tables": [
{
"TableName": "users",
"DeletedDateColumn": "deleted_at",
"RetentionDays": 30,
"Enabled": true
},
{
"TableName": "refresh_tokens",
"DeletedDateColumn": "deleted_at",
"RetentionDays": 7,
"Enabled": true
}
]
}
}
Configuration Options
Global Settings
| Property | Type | Default | Description |
|---|---|---|---|
Enabled | bool | true | Enable/disable the service globally |
CleanupIntervalHours | int | 24 | How often to run cleanup (hours) |
InitialDelayMinutes | int | 10 | Delay before first cleanup (minutes) |
Schema | string? | "public" | Database schema name |
Table-Specific Settings
| Property | Type | Default | Description |
|---|---|---|---|
TableName | string | required | Name of the table to clean |
DeletedDateColumn | string | "deleted_at" | Column storing deletion timestamp |
RetentionDays | int | 30 | Days to keep soft-deleted records |
Enabled | bool | true | Enable/disable for this table |
How It Works
- Soft Delete Detection: Finds records where
deleted_at(or configured column) is NOT NULL. - Retention Check: Compares deletion timestamp against retention period.
- Permanent Deletion: Removes records older than retention period using SQL DELETE.
- Scheduled Execution: Runs on configured interval (default: daily).
Custom Strategies
You can implement custom retention strategies by implementing IDataRetentionStrategy:
public interface IDataRetentionStrategy
{
Task<int> ExecuteAsync(
DbContext dbContext,
TableRetentionPolicy policy,
string? schema,
CancellationToken cancellationToken);
}