option('days'); $force = $this->option('force'); $log = Log::channel('jobs_clean'); $log->info('Starting job cleanup', ['days' => $days, 'force' => $force]); $this->info("Cleaning job listings..."); $this->newLine(); // Remove expired jobs $expiredCount = $this->removeExpiredJobs($force); // Remove old jobs (not posted in the last X days) $oldCount = $this->removeOldJobs($days, $force); // Clean up soft-deleted jobs $trashedCount = $this->cleanTrashedJobs($force); $this->newLine(); $total = $expiredCount + $oldCount + $trashedCount; $this->info("Cleanup Summary:"); $this->line(" • Expired jobs removed: {$expiredCount}"); $this->line(" • Old jobs removed (>{$days} days): {$oldCount}"); $this->line(" • Trashed jobs cleaned: {$trashedCount}"); $this->line(" • Total removed: {$total}"); $log->info('Cleanup complete', [ 'expired' => $expiredCount, 'old' => $oldCount, 'trashed' => $trashedCount, 'total' => $total, ]); return self::SUCCESS; } private function removeExpiredJobs(bool $force): int { $query = Job::whereNotNull('expires_at') ->where('expires_at', '<', now()); $count = $query->count(); if ($count === 0) { return 0; } if (!$force) { $this->line("Found {$count} expired jobs (soft deleting)..."); $query->delete(); // Soft delete } else { $this->line("Found {$count} expired jobs (force deleting)..."); $query->forceDelete(); // Permanent delete } return $count; } private function removeOldJobs(int $days, bool $force): int { $cutoffDate = now()->subDays($days); $query = Job::where(function ($q) use ($cutoffDate) { $q->where('posted_at', '<', $cutoffDate) ->orWhere(function ($q2) use ($cutoffDate) { $q2->whereNull('posted_at') ->where('created_at', '<', $cutoffDate); }); }); $count = $query->count(); if ($count === 0) { return 0; } if (!$force) { $this->line("Found {$count} old jobs (soft deleting)..."); $query->delete(); // Soft delete } else { $this->line("Found {$count} old jobs (force deleting)..."); $query->forceDelete(); // Permanent delete } return $count; } private function cleanTrashedJobs(bool $force): int { if (!$force) { return 0; } // Permanently delete jobs that have been soft-deleted for more than 30 days $query = Job::onlyTrashed() ->where('deleted_at', '<', now()->subDays(30)); $count = $query->count(); if ($count > 0) { $this->line("Found {$count} trashed jobs older than 30 days (permanently deleting)..."); $query->forceDelete(); } return $count; } }