{"record":{"id":"791015fdc88ea15c","repo":"TechnitiumSoftware/DnsServer","slug":"failed-to-clone-the-zone-zone-already-exists","errorCode":null,"errorMessage":"Failed to clone the zone: zone already exists.","messagePattern":"Failed to clone the zone: zone already exists\\.","errorType":"exception","errorClass":"DnsServerException","httpStatus":null,"severity":"error","filePath":"DnsServerCore/Dns/ZoneManagers/AuthZoneManager.cs","lineNumber":1323,"sourceCode":"\n            AuthZoneInfo zoneInfo;\n\n            switch (sourceZoneInfo.Type)\n            {\n                case AuthZoneType.Primary:\n                    zoneInfo = CreatePrimaryZone(zoneName);\n                    break;\n\n                case AuthZoneType.Forwarder:\n                    zoneInfo = CreateForwarderZone(zoneName);\n                    break;\n\n                default:\n                    throw new DnsServerException(\"Cannot clone the zone: source zone must be a Primary or Conditional Forwarder zone.\");\n            }\n\n            if (zoneInfo is null)\n                throw new DnsServerException(\"Failed to clone the zone: zone already exists.\");\n\n            //copy zone options\n            zoneInfo.Disabled = sourceZoneInfo.Disabled;\n\n            if (zoneInfo.Type == AuthZoneType.Primary)\n            {\n                zoneInfo.ZoneTransfer = sourceZoneInfo.ZoneTransfer;\n                zoneInfo.ZoneTransferNetworkACL = sourceZoneInfo.ZoneTransferNetworkACL;\n                zoneInfo.ZoneTransferTsigKeyNames = sourceZoneInfo.ZoneTransferTsigKeyNames;\n\n                zoneInfo.Notify = sourceZoneInfo.Notify;\n                zoneInfo.NotifyNameServers = sourceZoneInfo.NotifyNameServers;\n\n                zoneInfo.Update = sourceZoneInfo.Update;\n                zoneInfo.UpdateNetworkACL = sourceZoneInfo.UpdateNetworkACL;\n\n                if (sourceZoneInfo.UpdateSecurityPolicies is not null)\n                {","sourceCodeStart":1305,"sourceCodeEnd":1341,"githubUrl":"https://github.com/TechnitiumSoftware/DnsServer/blob/d0484b6c1e7439cdc53d67d81e9c876cda2ad756/DnsServerCore/Dns/ZoneManagers/AuthZoneManager.cs#L1305-L1341","documentation":"Thrown by CloneZone when the new-zone creation step (CreatePrimaryZone or CreateForwarderZone) returns null. Inside this library, those create methods return null specifically when a zone with the target name already exists, so this message names that condition. It fires after the source-type switch succeeds but before any options are copied.","triggerScenarios":"Calling CloneZone(zoneName, sourceZoneName) where zoneName already matches an existing authoritative zone. CreatePrimaryZone/CreateForwarderZone detect the duplicate via _root.TryAdd returning false and yield null, which the null-check at line 1322 turns into this exception.","commonSituations":"Re-running a clone script without deleting the prior target. UI auto-generating the clone name as '<source>-copy' which already exists. Cloning the same source twice in a loop.","solutions":["Pick a unique zoneName for the clone target, or delete the existing target zone first.","Pre-check with GetAuthZoneInfo(zoneName); if non-null, prompt the user to overwrite or rename.","Use a timestamped or counter-suffixed name generator for clone targets.","Make the clone operation idempotent by checking existence before invoking CloneZone."],"exampleFix":"// before\nmanager.CloneZone(\"copy.example.com\", sourceZoneName); // throws if target exists\n\n// after\nstring target = \"copy.example.com\";\nwhile (manager.GetAuthZoneInfo(target) is not null)\n    target = \"copy-\" + Guid.NewGuid().ToString(\"n\").Substring(0,4) + \".example.com\";\nmanager.CloneZone(target, sourceZoneName);","handlingStrategy":"validation","validationCode":"if (manager.GetAuthZoneInfo(targetName) is not null)\n    return Conflict($\"Target zone '{targetName}' already exists.\");","typeGuard":null,"tryCatchPattern":"catch (DnsServerException ex) when (ex.Message.Contains(\"already exists\")) { return Conflict(ex.Message); }","preventionTips":["Generate unique clone target names.","Pre-check target existence before cloning.","Make clone workflows idempotent."],"tags":["dns","zone-management","clone","duplicate","conflict"],"backgroundTag":null,"analyzedSha":"d0484b6c1e7439cdc53d67d81e9c876cda2ad756","analyzedAt":"2026-08-13T22:57:35.508Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}