spatie/laravel-directory-cleanup
Automatically delete old files from specified directories in Laravel. Configure per-path age limits (in minutes) via a published config file, then run cleanup to keep temp, cache, and upload folders tidy. Supports auto service provider registration in Laravel 5.5+.
/tmp, /storage/app/public/uploads)./storage/logs)./bootstrap/cache)./storage/app/media).config/directory-cleanup.php) allows granular control over retention policies per directory, aligning with 12-factor app principles for ephemeral storage.register, boot).php artisan directory:cleanup).config/, app/Console/).DirectoryCleanupWillRun, DirectoryCleanupDidRun).| Risk Area | Assessment | Mitigation Strategy |
|---|---|---|
| File Locking | Concurrent processes (e.g., uploads + cleanup) may cause race conditions. | Use flock() or Laravel’s Storage::lock() if critical; schedule cleanup during low-traffic periods. |
| Permission Issues | Cleanup may fail if PHP lacks write permissions on target directories. | Validate permissions pre-deployment; use chmod/chown in CI/CD or deployment scripts. |
| Accidental Deletion | Misconfigured retention policies could delete critical files. | Implement dry-run mode (--dry-run flag) and backup validation (e.g., log deleted files). |
| Performance | Large directories (e.g., millions of files) may slow Laravel. | Test with benchmark(); consider chunked processing or queue-based cleanup. |
| Storage Adapter | May not work seamlessly with non-local storage (e.g., S3). | Test with spatie/laravel-medialibrary or spatie/laravel-activitylog integrations. |
storage/logs/cleanup.log) for auditing?spatie/laravel-medialibrary for model-based cleanup.monolog for audit trails.CleanupDirectoriesJob) for async processing.Console component)./storage/app/public/uploads) for:
composer require spatie/laravel-directory-cleanup
php artisan vendor:publish --provider="Spatie\DirectoryCleanup\DirectoryCleanupServiceProvider"
config/directory-cleanup.php with test directories (e.g., non-critical logs).php artisan directory:cleanup --dry-run
app/Console/Kernel.php for scheduled cleanup:
protected function schedule(Schedule $schedule)
{
$schedule->command('directory:cleanup')->dailyAt('03:00');
}
laravel.log).| Component | Compatibility Notes |
|---|---|
| Laravel Version | Tested on v8+; may require minor adjustments for v9/10 (e.g., dependency updates). |
| PHP Version | Requires PHP 8.0+ (aligns with Laravel’s minimum). |
| Storage Adapters | Primarily local filesystems; S3/FTP support requires custom logic (e.g., League\Flysystem). |
| Database | No direct DB interaction, but cleanup of DB-backed uploads (e.g., medialibrary) needs coordination. |
| CI/CD | Safe for deployment; avoid running cleanup in CI (e.g., GitHub Actions). |
directory-cleanup.php.config/directory-cleanup.php).config/directory-cleanup-staging.php) for testing.spatie/laravel-package-tools).composer update spatie/laravel-directory-cleanup).--verbose flag) for debugging.storage directory permissions (chmod -R 755 storage).find or rsync for large directories.directory-cleanup.php for misconfigured paths.php artisan directory:restore).Cache::lock() to prevent concurrent executions.How can I help you explore Laravel packages today?