Console Commands
warden:clean
Section titled “warden:clean”The warden:clean command deletes unused abilities. Running this command will delete 2 types of unused abilities.
Unassigned Abilities
Section titled “Unassigned 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.
Orphaned Abilities
Section titled “Orphaned 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:
php artisan warden:cleanIf you only want to delete one type of unused ability, run it with one of the following flags:
php artisan warden:clean --unassignedphp artisan warden:clean --orphanedScheduling
Section titled “Scheduling”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();Examples
Section titled “Examples”Clean all unused abilities:
php artisan warden:cleanClean only unassigned abilities:
php artisan warden:clean --unassignedClean only orphaned abilities:
php artisan warden:clean --orphanedSchedule weekly cleanup in app/Console/Kernel.php:
use Illuminate\Console\Scheduling\Schedule;
protected function schedule(Schedule $schedule){ $schedule->command('warden:clean')->weekly();}