Skip to content

Console Commands

The warden:clean command deletes unused abilities. Running this command will delete 2 types of unused abilities.

Abilities that are not assigned to anyone. For example:

Warden::allow($user)->to('view', Plan::class);
Warden::disallow($user)->to('view', Plan::class);

At this point, the “view plans” ability is not assigned to anyone, so it’ll get deleted.

Note: depending on the context of your app, you may not want to delete these. If you let your users manage abilities in your app’s UI, you probably don’t want to delete unassigned abilities.

Model abilities whose models have been deleted:

Warden::allow($user)->to('delete', $plan);
$plan->delete();

Since the plan no longer exists, the ability is no longer of any use, so it’ll get deleted.

Run the command to delete both types of unused abilities:

Terminal window
php artisan warden:clean

If you only want to delete one type of unused ability, run it with one of the following flags:

Terminal window
php artisan warden:clean --unassigned
Terminal window
php artisan warden:clean --orphaned

To automatically run this command periodically, add it to your console kernel’s schedule:

protected function schedule(Schedule $schedule)
{
$schedule->command('warden:clean')->weekly();
}

Or run it daily:

$schedule->command('warden:clean')->daily();

Or run it monthly:

$schedule->command('warden:clean')->monthly();

Clean all unused abilities:

Terminal window
php artisan warden:clean

Clean only unassigned abilities:

Terminal window
php artisan warden:clean --unassigned

Clean only orphaned abilities:

Terminal window
php artisan warden:clean --orphaned

Schedule weekly cleanup in app/Console/Kernel.php:

use Illuminate\Console\Scheduling\Schedule;
protected function schedule(Schedule $schedule)
{
$schedule->command('warden:clean')->weekly();
}