Installation:
composer require vizrex/laravel-cleaner
Publish the config (if needed) with:
php artisan vendor:publish --provider="Vizrex\Cleaner\CleanerServiceProvider"
First Use Case: Run a basic cleanup for all registered commands:
php artisan app:cleanup --all
Verify the output to confirm which commands were executed (e.g., cache:clear, session:clear).
Key Files:
config/cleaner.php (if published) – Customize excluded files/directories.app/Console/Kernel.php – Check if the CleanerCommand is registered (it should be by default).Targeted Cleanup: Use flags to selectively clear specific caches or logs:
php artisan app:cleanup --log --session
Ideal for CI/CD pipelines or post-deployment cleanup.
Scheduled Cleanup:
Add a cron job or Laravel scheduler task (e.g., app/Console/Kernel.php) to run periodically:
$schedule->command('app:cleanup --log --session')->daily();
Useful for automated maintenance (e.g., clearing old logs weekly).
Custom Exclusions:
Extend the package by modifying config/cleaner.php to exclude sensitive files:
'excludes' => [
'storage/framework/sessions/*_important_session*',
],
Integration with Deployments:
Trigger cleanup post-deploy in deploy.php (Deployer) or afterDeploy hook (Envoyer):
run('php artisan app:cleanup --all');
env:switch:
Clear caches/logs when switching environments:
php artisan env:switch production && php artisan app:cleanup --all
php artisan app:cleanup --log > cleanup.log
Permission Issues:
storage/) has writable permissions for the web server user (e.g., chmod -R 775 storage/).--verbose to identify permission errors:
php artisan app:cleanup --log --verbose
Partial Cleanup:
--all flag runs all registered commands, including third-party ones (e.g., debugbar:clear). Test in a staging environment first.--log --session) to avoid unintended side effects.Log Rotation:
log:clear deletes all laravel*.log files. For production, consider:
LOG_MAX_FILES in .env).Session Data Loss:
session:clear deletes all session files. Avoid running this in production during active user sessions unless necessary.Dry Run:
Add --dry-run (if supported in future updates) or manually check the storage/framework/sessions/ and storage/logs/ directories before running cleanup.
Command Registration:
If a command (e.g., debugbar:clear) fails silently, verify it’s registered in app/Console/Kernel.php:
protected $commands = [
\Barryvdh\Debugbar\Console\ClearCommand::class, // Example for Debugbar
];
Custom Commands: Extend the package by adding new cleanup commands. Example:
// app/Console/Commands/CustomCleanup.php
use Vizrex\Cleaner\Traits\CleanupTrait;
class CustomCleanup extends Command {
use CleanupTrait;
protected $signature = 'custom:clean';
public function handle() {
$this->cleanDirectory(storage_path('app/custom_cache'));
}
}
Pre/Post Hooks:
Override the CleanerCommand to add logic before/after cleanup:
// app/Console/Commands/CleanerCommand.php
protected function execute(Input $input, Output $output) {
$this->info('Starting cleanup...');
parent::execute($input, $output);
$this->info('Cleanup complete.');
}
Config Overrides: Dynamically set exclusions via environment variables:
// config/cleaner.php
'excludes' => env('CLEANER_EXCLUDES', []),
Then run:
CLEANER_EXCLUDES="storage/framework/sessions/*_admin*" php artisan app:cleanup --session
How can I help you explore Laravel packages today?