option('dry-run'); // Update is_expired flag for files past expiration $this->updateExpiredFlags(); // Get expired files $expiredFiles = File::where('is_expired', true) ->whereNotNull('expires_at') ->where('expires_at', '<', now()) ->get(); $this->info("Found {$expiredFiles->count()} expired files"); foreach ($expiredFiles as $file) { if ($isDryRun) { $this->line("Would delete: {$file->filename_og}"); continue; } // Delete physical file try { Storage::disk($file->disk)->delete($file->storage_path); $this->info("Deleted file: {$file->filename_og}"); } catch (\Exception $e) { $this->error("Failed to delete file {$file->filename_og}: {$e->getMessage()}"); } // Soft delete database record $file->delete(); } if (!$isDryRun) { $this->info("Cleanup complete. Deleted {$expiredFiles->count()} files."); } return 0; } /** * Update is_expired flags for files past their expiration date */ protected function updateExpiredFlags() { $updated = File::whereNotNull('expires_at') ->where('expires_at', '<', now()) ->where('is_expired', false) ->update(['is_expired' => true]); if ($updated > 0) { $this->info("Updated {$updated} files as expired"); } } }