{"record":{"id":"4c1023966a88f1ec","repo":"TechnitiumSoftware/DnsServer","slug":"cannot-delete-private-key-no-such-private-key-was","errorCode":null,"errorMessage":"Cannot delete private key: no such private key was found.","messagePattern":"Cannot delete private key: no such private key was found\\.","errorType":"exception","errorClass":"DnsServerException","httpStatus":null,"severity":"error","filePath":"DnsServerCore/Dns/Zones/PrimaryZone.cs","lineNumber":1096,"sourceCode":"            {\n                if (!_dnssecPrivateKeys.TryGetValue(keyTag, out DnssecPrivateKey privateKey))\n                    throw new DnsServerException(\"Cannot update private key: no such private key was found.\");\n\n                privateKey.RolloverDays = rolloverDays;\n\n                return privateKey;\n            }\n        }\n\n        public void DeletePrivateKey(ushort keyTag)\n        {\n            if (_dnssecStatus == AuthZoneDnssecStatus.Unsigned)\n                throw new DnsServerException(\"The zone must be signed.\");\n\n            lock (_dnssecPrivateKeys)\n            {\n                if (!_dnssecPrivateKeys.TryGetValue(keyTag, out DnssecPrivateKey privateKey))\n                    throw new DnsServerException(\"Cannot delete private key: no such private key was found.\");\n\n                if (privateKey.State != DnssecPrivateKeyState.Generated)\n                    throw new DnsServerException(\"Cannot delete private key: only keys with Generated state can be deleted.\");\n\n                _dnssecPrivateKeys.Remove(keyTag);\n            }\n        }\n\n        public void PublishAllGeneratedKeys()\n        {\n            if (_dnssecStatus == AuthZoneDnssecStatus.Unsigned)\n                throw new DnsServerException(\"The zone must be signed.\");\n\n            List<DnssecPrivateKey> generatedPrivateKeys = new List<DnssecPrivateKey>();\n            List<DnsResourceRecord> newDnsKeyRecords = new List<DnsResourceRecord>();\n\n            uint dnsKeyTtl = GetDnsKeyTtl();\n","sourceCodeStart":1078,"sourceCodeEnd":1114,"githubUrl":"https://github.com/TechnitiumSoftware/DnsServer/blob/d0484b6c1e7439cdc53d67d81e9c876cda2ad756/DnsServerCore/Dns/Zones/PrimaryZone.cs#L1078-L1114","documentation":"Thrown by DeletePrivateKey when _dnssecPrivateKeys.TryGetValue fails for the supplied keyTag. You cannot delete a key the zone does not hold.","triggerScenarios":"Calling DeletePrivateKey with a keyTag absent from the zone's private key set.","commonSituations":"Retrying a delete after the key was already removed, or using a keyTag copied from a different zone.","solutions":["Verify the keyTag exists in zone.DnssecPrivateKeys before deleting.","Treat an absent keyTag as already-deleted if idempotency is desired (guard, don't call).","Refresh your key listing before acting on a user-supplied tag."],"exampleFix":"// before\nzone.DeletePrivateKey(keyTag);\n\n// after\nvar keys = zone.DnssecPrivateKeys;\nif (keys.Any(k => k.KeyTag == keyTag))\n    zone.DeletePrivateKey(keyTag);\nelse\n    Console.WriteLine($\"KeyTag {keyTag} not present; already deleted.\");","handlingStrategy":"validation","validationCode":"// Confirm the keyTag exists before deleting\nif (zone.DnssecPrivateKeys.Any(k => k.KeyTag == keyTag))\n    zone.DeletePrivateKey(keyTag);","typeGuard":"static bool KeyTagExists(ApexZone zone, ushort keyTag) =>\n    zone.DnssecPrivateKeys.Any(k => k.KeyTag == keyTag);","tryCatchPattern":"try\n{\n    zone.DeletePrivateKey(keyTag);\n}\ncatch (DnsServerException ex) when (ex.Message.Contains(\"no such private key was found\"))\n{\n    // key already gone; treat as success for idempotent cleanup\n}","preventionTips":["Make deletion idempotent by checking existence first.","Refresh the key list before acting on a user-supplied tag."],"tags":["dnssec","private-key","validation","csharp"],"backgroundTag":null,"analyzedSha":"d0484b6c1e7439cdc53d67d81e9c876cda2ad756","analyzedAt":"2026-08-13T22:57:35.508Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}