ipfs/kubo · error
key with name '%s' already exists
Error message
key with name '%s' already exists
What it means
`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.
Source
Thrown at core/commands/keystore.go:555
" use flag --%s if you are sure of what you're doing",
t, keyAllowAnyTypeOptionName)
}
}
cfgRoot, err := cmdenv.GetConfigRoot(env)
if err != nil {
return err
}
r, err := fsrepo.Open(cfgRoot)
if err != nil {
return err
}
defer r.Close()
_, err = r.Keystore().Get(name)
if err == nil {
return fmt.Errorf("key with name '%s' already exists", name)
}
err = r.Keystore().Put(name, sk)
if err != nil {
return err
}
pid, err := peer.IDFromPrivateKey(sk)
if err != nil {
return err
}
return cmds.EmitOnce(res, &KeyOutput{
Name: name,
Id: keyEnc.FormatID(pid),
})
},
Encoders: cmds.EncoderMap{View on GitHub (pinned to 329838acdf)
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
Example fix
// before $ ipfs key import mykey key.pem Error: key with name 'mykey' already exists // after $ ipfs key rm mykey $ ipfs key import mykey key.pem
Defensive patterns
Strategy: try-catch
Validate before calling
out, _ := exec.Command("ipfs", "key", "list", "--enc=json").Output()
var names []struct{ Name string }
json.Unmarshal(out, &names)
for _, n := range names {
if n.Name == targetName {
return fmt.Errorf("key %q exists; choose another name or run `ipfs key rm` first", targetName)
}
} Try / catch
err := importKey(name, file)
if err != nil && strings.Contains(err.Error(), "already exists") {
// pick a new name or explicitly rm the old key
} Prevention
- 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
When it happens
Trigger: `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`.
Common situations: 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.
Related errors
- expected PRIVATE KEY type in PEM block but got: %s
- parsing PKCS8 format: %w
- converting std Go key to libp2p key: %w
- unexpected PEM block for format=%s: try again with format=%s
- unable to unmarshall format=%s: %w
AI-assisted analysis of ipfs/kubo@329838acdf (2026-09-03).
Data as JSON: /api/errors/db7dbac2f9394324.
Report an issue: GitHub.