Skip to main content

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 IDataRetentionStrategy for 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

PropertyTypeDefaultDescription
EnabledbooltrueEnable/disable the service globally
CleanupIntervalHoursint24How often to run cleanup (hours)
InitialDelayMinutesint10Delay before first cleanup (minutes)
Schemastring?"public"Database schema name

Table-Specific Settings

PropertyTypeDefaultDescription
TableNamestringrequiredName of the table to clean
DeletedDateColumnstring"deleted_at"Column storing deletion timestamp
RetentionDaysint30Days to keep soft-deleted records
EnabledbooltrueEnable/disable for this table

How It Works

  1. Soft Delete Detection: Finds records where deleted_at (or configured column) is NOT NULL.
  2. Retention Check: Compares deletion timestamp against retention period.
  3. Permanent Deletion: Removes records older than retention period using SQL DELETE.
  4. 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);
}