whereNotNull('dns_record_id')->orderBy('name')->get(); if ($hosts->isEmpty()) { $this->info('No host carries a public dns_record_id. Nothing to do.'); return self::SUCCESS; } $force = (bool) $this->option('force'); $failed = 0; foreach ($hosts as $host) { $label = $host->name !== '' && $host->name !== null ? $host->name : $host->uuid; $recordId = $host->dns_record_id; if (! $force) { $this->line(" would delete {$label} record={$recordId}"); continue; } try { $dns->deleteRecord($recordId); $host->update(['dns_record_id' => null]); $this->info(" deleted {$label} record={$recordId}"); } catch (Throwable $e) { $failed++; $this->error(" failed {$label} record={$recordId}: {$e->getMessage()}"); } } if (! $force) { $this->newLine(); $this->comment(sprintf('Dry run — %d record(s) found, nothing changed. Re-run with --force to delete.', $hosts->count())); return self::SUCCESS; } return $failed === 0 ? self::SUCCESS : self::FAILURE; } }