{"record":{"id":"4b36de0f1e461923","repo":"TechnitiumSoftware/DnsServer","slug":"failed-to-delete-secondary-node-only-a-primary-no","errorCode":null,"errorMessage":"Failed to delete Secondary node: only a Primary node can delete a Secondary node from the Cluster.","messagePattern":"Failed to delete Secondary node: only a Primary node can delete a Secondary node from the Cluster\\.","errorType":"exception","errorClass":"DnsServerException","httpStatus":null,"severity":"error","filePath":"DnsServerCore/Cluster/ClusterManager.cs","lineNumber":770,"sourceCode":"            if (!_clusterNodes.TryGetValue(secondaryNodeId, out ClusterNode secondaryNode))\n                throw new DnsServerException(\"Failed to ask Secondary node to leave: the specified node does not exist in the Cluster.\");\n\n            if (secondaryNode.Type == ClusterNodeType.Primary)\n                throw new DnsServerException(\"Failed to ask Secondary node to leave: the specified node is the Cluster Primary node and cannot be removed.\");\n\n            //ask secondary node to leave the cluster\n            await secondaryNode.AskSecondaryNodeToLeaveClusterAsync();\n\n            return secondaryNode;\n        }\n\n        public ClusterNode DeleteSecondaryNode(int secondaryNodeId)\n        {\n            if (!ClusterInitialized)\n                throw new DnsServerException(\"Failed to delete Secondary node: the Cluster is not initialized.\");\n\n            if (GetSelfNode().Type != ClusterNodeType.Primary)\n                throw new DnsServerException(\"Failed to delete Secondary node: only a Primary node can delete a Secondary node from the Cluster.\");\n\n            //find existing secondary node\n            IReadOnlyDictionary<int, ClusterNode> existingClusterNodes = _clusterNodes;\n\n            if (!existingClusterNodes.TryGetValue(secondaryNodeId, out ClusterNode secondaryNode))\n                throw new DnsServerException(\"Failed to delete Secondary node: the specified node does not exist in the Cluster.\");\n\n            if (secondaryNode.Type == ClusterNodeType.Primary)\n                throw new DnsServerException(\"Failed to delete Secondary node: the specified node is the Cluster Primary node and cannot be deleted.\");\n\n            //delete secondary node from cluster nodes\n            Dictionary<int, ClusterNode> updatedClusterNodes = new Dictionary<int, ClusterNode>(existingClusterNodes.Count - 1);\n\n            foreach (KeyValuePair<int, ClusterNode> existingClusterNode in existingClusterNodes)\n            {\n                if (existingClusterNode.Key == secondaryNodeId)\n                    continue;\n","sourceCodeStart":752,"sourceCodeEnd":788,"githubUrl":"https://github.com/TechnitiumSoftware/DnsServer/blob/d0484b6c1e7439cdc53d67d81e9c876cda2ad756/DnsServerCore/Cluster/ClusterManager.cs#L752-L788","documentation":"Thrown by DeleteSecondaryNode when the current server's self node is not the Primary node. Only the Primary can remove a Secondary from the authoritative node list, update zone records, and rewrite the catalog zone.","triggerScenarios":"DeleteSecondaryNode is called from a Secondary node. The guard at line 769 fires after the ClusterInitialized check.","commonSituations":"The delete-secondary API request targets a Secondary server instead of the Primary; load balancer routing cluster-management calls to the wrong backend.","solutions":["Run DeleteSecondaryNode on the cluster Primary node.","Verify GetSelfNode().Type == ClusterNodeType.Primary before calling."],"exampleFix":"// before\n_clusterManager.DeleteSecondaryNode(nodeId);\n// after\nif (_clusterManager.GetSelfNode().Type != ClusterNodeType.Primary)\n    throw new InvalidOperationException(\"Run on the Primary node.\");\n_clusterManager.DeleteSecondaryNode(nodeId);","handlingStrategy":"validation","validationCode":"if (_dnsWebService.ClusterManager.GetSelfNode().Type != ClusterNodeType.Primary)\n    throw new InvalidOperationException(\"DeleteSecondaryNode must run on the Primary node.\");\n_dnsWebService.ClusterManager.DeleteSecondaryNode(nodeId);","typeGuard":"static bool IsPrimaryNode(ClusterManager cm)\n    => cm.ClusterInitialized && cm.GetSelfNode().Type == ClusterNodeType.Primary;","tryCatchPattern":"try\n{\n    _clusterManager.DeleteSecondaryNode(nodeId);\n}\ncatch (DnsServerException ex) when (ex.Message.Contains(\"only a Primary node can delete a Secondary\"))\n{\n    throw new InvalidOperationException(\"Redirect this request to the cluster Primary node.\", ex);\n}","preventionTips":["Send all delete-secondary requests to the Primary node.","Check GetSelfNode().Type before calling node-removal methods.","Pin cluster-management endpoints to the Primary in load-balanced environments."],"tags":["cluster","authorization","primary-node","delete-secondary","rbac"],"backgroundTag":null,"analyzedSha":"d0484b6c1e7439cdc53d67d81e9c876cda2ad756","analyzedAt":"2026-08-13T22:57:35.508Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}