Search by

t-labs-co / laravel-cleanup-data

t-labs-co

Centralized data-retention and cleanup framework for Laravel โ€” prune Eloquent models, clean storage files, run custom cleanup logic, and schedule operations automatically.

Package info

github.com/T-Labs-Co/laravel-cleanup-data

pkg:composer/t-labs-co/laravel-cleanup-data

Fund package maintenance!

ty-huynh

Statistics

Installs: 1

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

1.0.0 2026-10-21 11:25 UTC

This package is auto-updated.

Last update: 2026-10-21 11:46:21 UTC


README

Tests Latest Version License PHP

Centralized data-retention and cleanup framework for Laravel. Prune Eloquent models, clean storage files, run custom cleanup logic, and schedule operations โ€” all from a single config.

Features

  • ๐Ÿš€ Centralized Cleanup Registry: Register and execute cleanup actions with tag and alias support.
  • ๐Ÿงน Generic Model Pruner: Easily prune records from Eloquent models based on date/time columns via config or fluent builder.
  • ๐Ÿ“ Expired Files Cleaner: Clean up old files from storage disks and directories automatically.
  • โšก Fluent API & Facade: Convenient Cleaner::model(User::class)->days(30)->clean() API for ad-hoc cleanup.
  • ๐Ÿ” Dry-Run Mode: Safely preview items that would be deleted without touching real data.
  • โฐ Built-in Scheduling: Config-driven automated cron scheduling.
  • ๐Ÿช Hook & Logging System: Lifecycle hooks (before, after, logging) for comprehensive observability and error tracking.

Requirements

  • PHP 8.4+
  • Laravel 12.0+ or 13.0+

Installation

composer require t-labs-co/laravel-cleanup-data

Publish configuration file:

php artisan vendor:publish --tag=laravel-cleanup-data-config

Quick Start (3 minutes)

1. Fluent Facade Pruning

use TLabsCo\LaravelCleanupData\Facades\Cleaner;
use App\Models\ActivityLog;

// Delete soft-deletable or standard records older than 30 days
$result = Cleaner::model(ActivityLog::class)
    ->days(30)
    ->clean();

// Preview without deleting
$preview = Cleaner::model(ActivityLog::class)
    ->days(30)
    ->dryRun();

2. Config-Driven Model Pruning

In config/laravel-cleanup-data.php:

'cleaners' => [
    'prune_models' => [
        [
            'name'    => 'activity-logs',
            'model'   => App\Models\ActivityLog::class,
            'column'  => 'created_at',
            'days'    => 30,
            'force'   => false,
            'enabled' => true,
        ],
    ],
],

3. Artisan Commands

# Run all enabled cleaners
php artisan cleanup:run

# Preview cleanup in dry-run mode
php artisan cleanup:run --dry-run

# Run specific tag or cleaner
php artisan cleanup:run --tag=storage --force
php artisan cleanup:run --cleaner=generic-models

# List all registered cleaners
php artisan cleanup:list

Documentation

Creating Custom Cleaners

Extend TLabsCo\LaravelCleanupData\Core\Cleaners\BaseCleaner and implement cleanup():

namespace App\Cleaners;

use TLabsCo\LaravelCleanupData\Core\Cleaners\BaseCleaner;
use TLabsCo\LaravelCleanupData\Core\ValueObject\CleanupResult;

class TempExportCleaner extends BaseCleaner
{
    protected array $tags = ['storage', 'exports', 'daily'];

    public function getName(): string
    {
        return 'Temp Export Cleaner';
    }

    public function getDescription(): string
    {
        return 'Prunes temporary export files older than 7 days';
    }

    protected function getConfigPrefix(): string
    {
        return 'temp_exports';
    }

    public function cleanup(bool $dryRun = false): CleanupResult
    {
        // Custom cleanup logic...
        return CleanupResult::success(
            cleaner: $this->getName(),
            processed: 15,
            deleted: $dryRun ? 0 : 15,
            message: 'Cleaned 15 temporary exports'
        );
    }
}

Register your custom cleaner in config/laravel-cleanup-data.php:

'cleaners_classes' => [
    \App\Cleaners\TempExportCleaner::class => [
        'alias' => 'temp-exports',
        'config_key' => 'temp_exports',
    ],
],

Configuration Reference

Refer to config/laravel-cleanup-data.php for complete configuration options including logging channel setup, failure reporting, schedule cron expression, and disk folders.

Contributing

Please feel free to submit issues or pull requests.

License

The MIT License (MIT). Please see License File for more information.

Credits

  • Ty Huynh (@ty-huynh) โ€” tyhuynh.id.vn
    15+ years ยท Senior Software Engineer (PHP/Laravel) & Technical Lead