t-labs-co / laravel-cleanup-data
Centralized data-retention and cleanup framework for Laravel โ prune Eloquent models, clean storage files, run custom cleanup logic, and schedule operations automatically.
Fund package maintenance!
Requires
- php: ^8.4
- illuminate/console: ^12|^13
- illuminate/filesystem: ^12|^13
- illuminate/support: ^12|^13
Requires (Dev)
- larastan/larastan: ^3
- laravel/pint: ^1
- orchestra/testbench: ^10|^11
- pestphp/pest: ^4|^5
- pestphp/pest-plugin-laravel: ^4|^5
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
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