visavi/cleanup
A Laravel package for automating cleanup of old or unused files and directories. Configure paths and retention rules, then run the provided Artisan command manually or on a schedule to reclaim disk space and keep application storage tidy.
composer require visavi/cleanup
config/cleanup.php:php artisan vendor:publish --tag=cleanup-config
php artisan list | grep -i cleanup
php artisan help cleanup
Open config/cleanup.php first. The important settings are usually:
Run it manually once in a safe environment:
php artisan cleanup -v
If the command supports a dry run, use that first:
php artisan cleanup --dry-run
// routes/console.php
use Illuminate\Support\Facades\Schedule;
Schedule::command('cleanup')
->dailyAt('02:30')
->withoutOverlapping()
->onOneServer();
Keep the first rollout conservative: enable only logs and temp-file cleanup, keep retention generous, then tighten after observing disk usage and command output.
If the command exposes target options, avoid one giant cleanup run for everything. Use separate schedules with different retention windows:
Schedule::command('cleanup --logs --days=30')
->dailyAt('02:00')
->withoutOverlapping();
Schedule::command('cleanup --temp --days=7')
->hourly()
->withoutOverlapping();
Schedule::command('cleanup --sessions --days=2')
->dailyAt('03:00')
->withoutOverlapping();
Confirm the actual option names with php artisan help cleanup. If the command only supports config-driven cleanup, create separate config profiles or use environment-specific config values instead.
Prefer configuration over hard-coded values:
// config/cleanup.php
return [
'logs' => [
'enabled' => true,
'days' => env('CLEANUP_LOG_DAYS', 14),
],
'temp' => [
'enabled' => true,
'days' => env('CLEANUP_TEMP_DAYS', 7),
],
'paths' => [
storage_path('app/tmp'),
storage_path('framework/cache/data'),
// Add app-specific scratch directories here.
],
];
This lets ops tune retention per environment without a deployment.
Use the package for application scratch data, generated exports, old queue payloads, or temporary uploads only after verifying the path is not user-facing or permanent:
'paths' => [
storage_path('app/exports/tmp'),
storage_path('app/imports/tmp'),
storage_path('framework/testing'),
],
Keep permanent storage such as storage/app/public out of cleanup paths unless you have a very explicit retention policy.
Useful patterns:
Example deployment hook:
php artisan cleanup --temp --logs -v
In tests, point cleanup paths at a fake disk or temporary directory and assert both deletion and preservation:
use Illuminate\Support\Facades\Storage;
Storage::fake('local');
Storage::disk('local')->put('tmp/old.txt', 'old');
Storage::disk('local')->put('tmp/new.txt', 'new');
$this->artisan('cleanup --temp --days=1')
->assertExitCode(0);
Storage::disk('local')->assertMissing('tmp/old.txt');
Storage::disk('local')->assertExists('tmp/new.txt');
Adjust the command signature to match the package.
Use the package for retention-based pruning, and Laravel’s built-in commands for framework-specific resets:
php artisan cache:clear
php artisan view:clear
php artisan cleanup --logs --temp
Do not duplicate responsibilities blindly. For example, Laravel’s log rotation may already handle daily logs; the package is more useful for aged files, temp directories, and custom application paths.
php artisan list | grep cleanup and php artisan help <command> before adding scheduler entries.storage_path()/base_path(). Avoid relative paths whose meaning depends on the current working directory.config/cleanup.php, run php artisan config:clear in local/staging. In production, deploy with the normal config:cache process.Schedule::command(...) does nothing without Laravel’s scheduler running. Confirm the server has the standard * * * * * php artisan schedule:run entry.withoutOverlapping() and onOneServer() deliberately. onOneServer() requires a shared cache store. If the cache is file-based, it will not provide cross-server coordination.storage/framework/cache, storage/framework/sessions, and storage/logs if the application is under load. Prefer Laravel-native commands for these unless the package explicitly supports safe pruning.php artisan cleanup --logs --temp -v for on-call use when disk usage spikes.How can I help you explore Laravel packages today?