{"record":{"id":"db7dbac2f9394324","repo":"ipfs/kubo","slug":"key-with-name-s-already-exists","errorCode":null,"errorMessage":"key with name '%s' already exists","messagePattern":"key with name '(.+?)' already exists","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"core/commands/keystore.go","lineNumber":555,"sourceCode":"\t\t\t\t\t\" use flag --%s if you are sure of what you're doing\",\n\t\t\t\t\tt, keyAllowAnyTypeOptionName)\n\t\t\t}\n\t\t}\n\n\t\tcfgRoot, err := cmdenv.GetConfigRoot(env)\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\n\t\tr, err := fsrepo.Open(cfgRoot)\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\t\tdefer r.Close()\n\n\t\t_, err = r.Keystore().Get(name)\n\t\tif err == nil {\n\t\t\treturn fmt.Errorf(\"key with name '%s' already exists\", name)\n\t\t}\n\n\t\terr = r.Keystore().Put(name, sk)\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\n\t\tpid, err := peer.IDFromPrivateKey(sk)\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\n\t\treturn cmds.EmitOnce(res, &KeyOutput{\n\t\t\tName: name,\n\t\t\tId:   keyEnc.FormatID(pid),\n\t\t})\n\t},\n\tEncoders: cmds.EncoderMap{","sourceCodeStart":537,"sourceCodeEnd":573,"githubUrl":"https://github.com/ipfs/kubo/blob/329838acdfafae224582930457efe80aa217afc0/core/commands/keystore.go#L537-L573","documentation":"`ipfs key import` refuses to overwrite an existing key in the node's keystore. Before storing, it looks up the target name; if a key with that name already exists, the import is aborted to protect the existing identity.","triggerScenarios":"`ipfs key import mykey key.pem` when `ipfs key list` already shows `mykey` (previously generated, imported, or a leftover); also importing with a name colliding with a key created by `ipfs key gen`.","commonSituations":"Re-running an import script that already succeeded once; forgetting that a previous `key gen` used the same name; restoring keys into a repo that already has them.","solutions":["Pick a new name: `ipfs key import mykey2 key.pem`","Remove the existing key first with `ipfs key rm mykey`, then re-import (the old key/identity is lost — export it first if needed)","Check existing names with `ipfs key list` before importing"],"exampleFix":"// before\n$ ipfs key import mykey key.pem\nError: key with name 'mykey' already exists\n// after\n$ ipfs key rm mykey\n$ ipfs key import mykey key.pem","handlingStrategy":"try-catch","validationCode":"out, _ := exec.Command(\"ipfs\", \"key\", \"list\", \"--enc=json\").Output()\nvar names []struct{ Name string }\njson.Unmarshal(out, &names)\nfor _, n := range names {\n    if n.Name == targetName {\n        return fmt.Errorf(\"key %q exists; choose another name or run `ipfs key rm` first\", targetName)\n    }\n}","typeGuard":null,"tryCatchPattern":"err := importKey(name, file)\nif err != nil && strings.Contains(err.Error(), \"already exists\") {\n    // pick a new name or explicitly rm the old key\n}","preventionTips":["Run `ipfs key list` before importing to check for name collisions","Make import scripts idempotent: check-then-import or use unique names","Export an existing key before removing it"],"tags":["keystore","name-conflict","key-import","duplicate"],"backgroundTag":"key-already-exists","analyzedSha":"329838acdfafae224582930457efe80aa217afc0","analyzedAt":"2026-09-03T18:30:52.135Z","contentChangedAt":"2026-09-03T18:30:52.135Z","schemaVersion":2},"datasetVersion":"2026-09-11T00:17:11.886Z"}