omaralalwi/laravel-trash-cleaner
composer require omaralalwi/laravel-trash-cleaner to add the package to your Laravel project.php artisan trash:clean to immediately clean debug files (Clockwork, Debugbar) and compiled caches.config/trash-cleaner.php (after running php artisan vendor:publish --tag=laravel-trash-cleaner) to verify default paths.Use this package post-deployment or in a CI/CD pipeline to ensure no stale debug files or caches persist in production. Example:
# Run in a deploy script or cron job
php artisan trash:clean --debug --force
--debug shows verbose output.--force skips confirmation prompts.Scheduled Cleanup
Add to app/Console/Kernel.php to run nightly:
protected function schedule(Schedule $schedule)
{
$schedule->command('trash:clean')->dailyAt('2:00');
}
Post-Deployment Hook Integrate with Laravel Forge/Envoyer or custom scripts:
# Example in a deploy script
php artisan trash:clean --assets --force
--assets clears frontend build directories (e.g., public/build).Custom Cleanup Logic Extend the package by binding your own cleanup logic:
// app/Providers/TrashCleanerServiceProvider.php
public function boot()
{
TrashCleaner::extend('custom', function () {
// Add your custom cleanup logic here
File::delete(storage_path('custom-cache/*.cache'));
});
}
Then call:
php artisan trash:clean --custom
storage/framework/views and database/telescope_entries to the paths array in config.--assets flag to trigger rebuilds (if configured) after cleaning:
php artisan trash:clean --assets --rebuild
npm, yarn, or pnpm to be installed on the server.Permission Issues
storage/) has writable permissions (chmod -R 755 storage/).775 permissions for group access.Missing Dependencies
--assets --rebuild, verify npm, yarn, or pnpm is installed and accessible in the PATH.Command "npm run dev" not found → Install Node.js or configure the correct path in trash-cleaner.php.Overzealous Cleaning
trash:clean during development if you rely on cached views or debug files.--dry-run to preview files before deletion:
php artisan trash:clean --dry-run
Config Overrides
trash-cleaner.php are misconfigured, the package may silently skip directories.php artisan trash:clean --debug
--debug to see which files are being processed.storage/logs/laravel.log for permission or path-related issues.TrashCleaner::extend('log', function () {
Log::info('Custom cleanup triggered', ['action' => 'log_cleanup']);
});
Custom Directories
Add new directories to the paths array in trash-cleaner.php:
'paths' => [
storage_path('logs'),
storage_path('framework/views'),
// Add your custom path
base_path('custom-cache'),
],
Pre/Post Hooks
Use Laravel’s registering and booting events in a service provider to run logic before/after cleanup:
TrashCleaner::registering(function () {
// Pre-cleanup logic (e.g., backup files)
});
TrashCleaner::booting(function () {
// Post-cleanup logic (e.g., notify Slack)
});
Conditional Cleanup Dynamically enable/disable cleanup based on environment:
// In a service provider
if (app()->environment('production')) {
TrashCleaner::enable();
}
How can I help you explore Laravel packages today?