PHP code example of iamankitthapar / laravel-data-retention

1. Go to this page and download the library: Download iamankitthapar/laravel-data-retention library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

iamankitthapar / laravel-data-retention example snippets


Retention::rule('old-api-logs')
    ->model(ApiLog::class)
    ->olderThan('created_at', '6 months')
    ->action('delete')
    ->limit(100);



namespace App\Providers;

use App\Models\ApiLog;
use Iamankitthapar\DataRetention\Facades\Retention;
use Illuminate\Support\ServiceProvider;

class RetentionServiceProvider extends ServiceProvider
{
    public function boot(): void
    {
        Retention::rule('old-api-logs')
            ->model(ApiLog::class)
            ->olderThan('created_at', '6 months')
            ->action('delete')
            ->limit(10);
    }
}

App\Providers\RetentionServiceProvider::class,



return [
    App\Providers\AppServiceProvider::class,
    App\Providers\RetentionServiceProvider::class,
];

$log = new App\Models\ApiLog;
$log->method = 'GET';
$log->path = '/example';
$log->status_code = 200;
$log->created_at = now()->subMonths(7);
$log->updated_at = $log->created_at;
$log->save();

Retention::rule('old-api-logs')
    ->model(ApiLog::class)
    ->olderThan('created_at', '6 months')
    ->action('delete')
    ->limit(100);

Retention::rule('old-user-profiles')
    ->model(User::class)
    ->olderThan('created_at', '2 years')
    ->anonymize([
        'name' => 'Anonymous User',
        'email' => ['strategy' => 'email'],
    ])
    ->limit(100);

->anonymize([
    'name' => 'Deleted user',
    'email' => fn (User $user) => "deleted-{$user->id}@example.invalid",
    'phone' => null,
])

Retention::rule('old-orders')
    ->model(Order::class)
    ->olderThan('created_at', '7 years')
    ->archive(['table' => 'orders_archive'])
    ->limit(100);

->archive([
    'connection' => 'archive',
    'table' => 'orders_archive',
])

->where('status', 'closed')
->olderThan('created_at', '1 year')

->exclude(fn ($query) => $query->where('legal_hold', true))

->olderThan('created_at', '1 year')
->gracePeriod('30 days')

->>onlySoftDeleted()    // Match only soft-deleted records.

->connection('mysql')

->schedule('daily')

->schedule('cron:0 2 * * *', 'Asia/Kolkata')

use Iamankitthapar\DataRetention\Facades\Retention;

Retention::schedule();
Retention::scheduleStoredPolicies();

use Iamankitthapar\DataRetention\Facades\Retention;
use Illuminate\Console\Scheduling\Schedule;

protected function schedule(Schedule $schedule): void
{
    Retention::schedule($schedule);
    Retention::scheduleStoredPolicies($schedule);
}
bash
composer vendor:publish --tag=data-retention-config
php artisan vendor:publish --tag=data-retention-migrations
php artisan migrate
bash
php artisan retention:install-example
php artisan migrate
bash
php artisan make:provider RetentionServiceProvider
bash
php artisan retention:list
php artisan retention:preview old-api-logs
bash
php artisan retention:run old-api-logs
php artisan retention:status
bash
php artisan schedule:list
bash
php artisan schedule:work
bash
php artisan retention:status
php artisan retention:report --format=json
php artisan retention:report --from="2026-01-01 00:00:00" --to="2026-01-31 23:59:59"
bash
php artisan retention:cleanup --days=90
bash
php artisan retention:policy:export --path=retention-policies.json
php artisan retention:policy:import retention-policies.json --replace