option('hours'); $dryRun = $this->option('dry-run'); $this->info("Pruning crawlshot storage files older than {$hours} hours..."); if ($dryRun) { $this->warn('DRY RUN MODE - No files will actually be deleted'); } // Find jobs older than specified hours $cutoffTime = Carbon::now()->subHours($hours); $oldJobs = CrawlShotJob::where('created_at', '<', $cutoffTime) ->whereNotNull('file_path') ->get(); if ($oldJobs->isEmpty()) { $this->info('No files found to prune.'); return Command::SUCCESS; } $this->info("Found {$oldJobs->count()} files to prune:"); $deletedFiles = 0; $deletedRecords = 0; $errors = 0; foreach ($oldJobs as $job) { $this->line("- {$job->type} job {$job->uuid} ({$job->file_path})"); if (!$dryRun) { // Delete the file if it exists if ($job->file_path && file_exists($job->file_path)) { if (unlink($job->file_path)) { $deletedFiles++; } else { $this->error(" Failed to delete file: {$job->file_path}"); $errors++; } } // Delete the database record try { $job->delete(); $deletedRecords++; } catch (\Exception $e) { $this->error(" Failed to delete database record: {$e->getMessage()}"); $errors++; } } } if (!$dryRun) { $this->info("Cleanup completed:"); $this->line(" - Files deleted: {$deletedFiles}"); $this->line(" - Database records deleted: {$deletedRecords}"); if ($errors > 0) { $this->error(" - Errors encountered: {$errors}"); return Command::FAILURE; } } else { $this->info("Would have deleted {$oldJobs->count()} files and records"); } return Command::SUCCESS; } }