pachristos / filament-export-cleanup
Clean up old Filament export files and database records.
Package info
github.com/Christos-Papoulas/filament-export-cleanup
pkg:composer/pachristos/filament-export-cleanup
Fund package maintenance!
Requires
- php: ^8.2
- filament/actions: ^4.0|^5.0
- illuminate/console: ^11.0|^12.0|^13.0
- illuminate/support: ^11.0|^12.0|^13.0
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.8
- orchestra/testbench: ^9.0|^10.0|^11.0
- pestphp/pest: ^3.0|^4.0
- pestphp/pest-plugin-arch: ^3.0|^4.0
- pestphp/pest-plugin-laravel: ^3.0|^4.0
README
Automatically remove old Filament export files from disk and optionally delete their exports table rows.
Filament table exports store files on disk and keep metadata in the database. Over time these accumulate. This package finds completed exports older than a configurable retention period, deletes their file directories, and optionally removes the database records.
Requirements
- PHP 8.2+
- Laravel 11, 12 or 13
- Filament Actions (exports) v4 or v5
Installation
Install the package via Composer:
composer require pachristos/filament-export-cleanup
Publish the configuration file:
php artisan vendor:publish --tag="filament-export-cleanup-config"
Or use the install command, which publishes the config for you:
php artisan filament-export-cleanup:install
The package auto-registers its service provider. No further setup is required beyond configuration.
Configuration
Configuration lives in config/filament-export-cleanup.php. All options can be overridden with environment variables.
| Option | Env variable | Default | Description |
|---|---|---|---|
enabled |
FILAMENT_EXPORT_CLEANUP_ENABLED |
true |
Master switch for cleanup |
retention_hours |
FILAMENT_EXPORT_CLEANUP_RETENTION_HOURS |
72 |
Delete exports completed more than this many hours ago |
delete_database_records |
FILAMENT_EXPORT_CLEANUP_DELETE_DATABASE_RECORDS |
true |
Also delete rows from the exports table |
file_disk |
FILAMENT_EXPORT_CLEANUP_FILE_DISK |
local |
Filesystem disk to clean (must match your Filament export disk) |
schedule.enabled |
FILAMENT_EXPORT_CLEANUP_SCHEDULE_ENABLED |
true |
Register the cleanup command on the Laravel scheduler |
schedule.frequency |
— | Weekdays at 02:00 |
When to run (see Scheduling) |
Set file_disk to the same disk your Filament exports use (local or public). S3 and other remote disks are not supported yet.
Usage
Scheduled cleanup (recommended)
When schedule.enabled is true, the package registers cleanup:filament-export on Laravel's scheduler. Ensure your server runs the scheduler (for example via cron):
* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1
The default schedule runs on weekdays at 02:00.
Manual cleanup
Run the Artisan command at any time:
php artisan cleanup:filament-export
The command reports how many export file directories were removed, or that nothing needed deletion.
Programmatic cleanup
Resolve the service from the container:
use Pachristos\FilamentExportCleanup\FilamentExportCleanup; $deletedIds = app(FilamentExportCleanup::class)->run();
Or use the facade:
use FilamentExportCleanup; $deletedIds = FilamentExportCleanup::run();
run() returns an array of deleted export IDs. When enabled is false, it returns an empty array without doing any work.
Scheduling
The schedule.frequency config controls when the cleanup command is registered on Laravel's scheduler. You can configure it with any of Laravel's schedule frequency options by passing a Closure that receives the scheduled Event and chains any combination of frequency methods (dailyAt(), hourly(), weekdays(), cron(), timezone constraints, etc.). A raw cron expression string is also accepted.
The default runs the cleanup on weekdays at 02:00:
use Illuminate\Console\Scheduling\Event; 'schedule' => [ 'enabled' => env('FILAMENT_EXPORT_CLEANUP_SCHEDULE_ENABLED', true), 'frequency' => fn (Event $event) => $event ->weekdays() ->dailyAt('02:00'), ],
Use any frequency method (or combination) Laravel supports. A few examples:
'frequency' => fn (Event $event) => $event->hourly(), 'frequency' => fn (Event $event) => $event ->twiceDaily(1, 13) ->timezone('Europe/Athens'), 'frequency' => fn (Event $event) => $event ->weeklyOn(0, '03:00'),
Or supply a raw cron expression directly:
'frequency' => '0 3 * * 0',
Disable automatic scheduling and run cleanup only manually or from your own scheduler:
FILAMENT_EXPORT_CLEANUP_SCHEDULE_ENABLED=false
See the Laravel schedule frequency options documentation for the full list of available methods.
How it works
- Query the Filament
exportstable for records wherecompleted_atis older thanretention_hoursandfile_diskmatches your configured disk. - Delete each export's file directory from disk via Filament's
Exportmodel. - If
delete_database_recordsistrue, delete the matching rows from theexportstable.
Exports still within the retention window are left untouched.
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Sponsoring
Development of this package is supported by Christos Papoulas.
License
The MIT License (MIT). Please see License File for more information.