<?php
namespace Laravel\Sanctum\Console\Commands;
use Illuminate\Console\Command;
use Laravel\Sanctum\Sanctum;
use Symfony\Component\Console\Attribute\AsCommand;
class PruneExpired extends Command
{
protected $signature = 'sanctum:prune-expired {--hours=24 : The number of hours to retain expired Sanctum tokens}';
protected $description = 'Prune tokens expired for more than specified number of hours';
public function handle()
{
$model = Sanctum::$personalAccessTokenModel;
$hours = $this->option('hours');
$this->components->task(
'Pruning tokens with expired expires_at timestamps',
fn () => $model::where('expires_at', '<', now()->subHours($hours))->delete()
);
if ($expiration = config('sanctum.expiration')) {
$this->components->task(
'Pruning tokens with expired expiration value based on configuration file',
fn () => $model::where('created_at', '<', now()->subMinutes($expiration + ($hours * 60)))->delete()