{"record":{"id":"3fec3a78e94681c8","repo":"ipfs/kubo","slug":"key-with-name-s-already-exists-3fec3a","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/coreapi/key.go","lineNumber":73,"sourceCode":"\n// Generate generates new key, stores it in the keystore under the specified\n// name and returns a base58 encoded multihash of its public key.\nfunc (api *KeyAPI) Generate(ctx context.Context, name string, opts ...caopts.KeyGenerateOption) (coreiface.Key, error) {\n\t_, span := tracing.Span(ctx, \"CoreAPI.KeyAPI\", \"Generate\", trace.WithAttributes(attribute.String(\"name\", name)))\n\tdefer span.End()\n\n\toptions, err := caopts.KeyGenerateOptions(opts...)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\tif name == \"self\" {\n\t\treturn nil, errors.New(\"cannot create key with name 'self'\")\n\t}\n\n\t_, err = api.repo.Keystore().Get(name)\n\tif err == nil {\n\t\treturn nil, fmt.Errorf(\"key with name '%s' already exists\", name)\n\t}\n\n\tif err := caopts.CheckKeySize(options.Algorithm, options.Size); err != nil {\n\t\treturn nil, err\n\t}\n\n\tvar sk crypto.PrivKey\n\tvar pk crypto.PubKey\n\n\tswitch options.Algorithm {\n\tcase \"rsa\":\n\t\tif options.Size == -1 {\n\t\t\toptions.Size = caopts.DefaultRSALen\n\t\t}\n\n\t\tpriv, pub, err := crypto.GenerateKeyPairWithReader(crypto.RSA, options.Size, rand.Reader)\n\t\tif err != nil {\n\t\t\treturn nil, err","sourceCodeStart":55,"sourceCodeEnd":91,"githubUrl":"https://github.com/ipfs/kubo/blob/329838acdfafae224582930457efe80aa217afc0/core/coreapi/key.go#L55-L91","documentation":"Fired by CoreAPI.Generate when a new IPNS key is requested under a name that already exists in the repo keystore. Key names must be unique; the caller (e.g. `ipfs key gen`) must choose another name or delete/overwrite the existing key explicitly.","triggerScenarios":"Calling KeyAPI.Generate(ctx, name) (or `ipfs key gen name ...`) with a name that already exists in the repo keystore.","commonSituations":"Re-running an idempotent-looking provisioning script that re-issues `ipfs key gen` with the same name; a failed prior gen that left the key in place; two concurrent key gens with the same name.","solutions":["Choose a different, unused key name","Remove the existing key first with 'ipfs key rm <name>' if it is no longer needed","List existing names with 'ipfs key list -l' to avoid collisions"],"exampleFix":"// before\nk, err := api.Key().Generate(ctx, \"mykey\")\n// after\nkeys, _ := api.Key().List(ctx)\nif !keyExists(keys, \"mykey\") {\n    k, err = api.Key().Generate(ctx, \"mykey\")\n}","handlingStrategy":"validation","validationCode":"keys, err := api.Key().List(ctx)\nif err != nil { return err }\nexists := slices.ContainsFunc(keys, func(k coreiface.Key) bool { return k.Name() == name })\nif exists {\n    return fmt.Errorf(\"key %q already exists; reuse or remove it first\", name)\n}","typeGuard":null,"tryCatchPattern":"k, err := api.Key().Generate(ctx, name)\nif err != nil && strings.Contains(err.Error(), \"already exists\") {\n    return nil // idempotent scripts: treat as success\n}","preventionTips":["Check for existing names before generating (List or Keystore().Get)","Make provisioning scripts idempotent (generate only if missing)","Avoid concurrent key generation with the same name"],"tags":["keystore","keys","duplicate"],"backgroundTag":"name-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"}