referencedPaths(); $cutoff = now()->subHours((int) $this->option('hours'))->getTimestamp(); $deleted = 0; foreach ($disk->files(CustomFieldUploadController::DIRECTORY) as $path) { if (isset($referenced[$path]) || $disk->lastModified($path) > $cutoff) { continue; } $disk->delete($path); $deleted++; } $this->info("Deleted {$deleted} unreferenced custom-field upload(s)."); return self::SUCCESS; } /** * @return array */ private function referencedPaths(): array { $paths = []; foreach ([CartLine::class, OrderLine::class] as $model) { $model::query() ->where('meta', 'like', '%custom_fields%') ->select('meta') ->cursor() ->each(function ($line) use (&$paths) { foreach ($line->meta['custom_fields'] ?? [] as $field) { if (! empty($field['path'])) { $paths[$field['path']] = true; } } }); } return $paths; } }