ipfs/kubo · error

key with name '%s' already exists

Error message

key with name '%s' already exists

What it means

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.

Source

Thrown at core/coreapi/key.go:73

// Generate generates new key, stores it in the keystore under the specified
// name and returns a base58 encoded multihash of its public key.
func (api *KeyAPI) Generate(ctx context.Context, name string, opts ...caopts.KeyGenerateOption) (coreiface.Key, error) {
	_, span := tracing.Span(ctx, "CoreAPI.KeyAPI", "Generate", trace.WithAttributes(attribute.String("name", name)))
	defer span.End()

	options, err := caopts.KeyGenerateOptions(opts...)
	if err != nil {
		return nil, err
	}

	if name == "self" {
		return nil, errors.New("cannot create key with name 'self'")
	}

	_, err = api.repo.Keystore().Get(name)
	if err == nil {
		return nil, fmt.Errorf("key with name '%s' already exists", name)
	}

	if err := caopts.CheckKeySize(options.Algorithm, options.Size); err != nil {
		return nil, err
	}

	var sk crypto.PrivKey
	var pk crypto.PubKey

	switch options.Algorithm {
	case "rsa":
		if options.Size == -1 {
			options.Size = caopts.DefaultRSALen
		}

		priv, pub, err := crypto.GenerateKeyPairWithReader(crypto.RSA, options.Size, rand.Reader)
		if err != nil {
			return nil, err

View on GitHub (pinned to 329838acdf)

Solutions

  1. Choose a different, unused key name
  2. Remove the existing key first with 'ipfs key rm <name>' if it is no longer needed
  3. List existing names with 'ipfs key list -l' to avoid collisions

Example fix

// before
k, err := api.Key().Generate(ctx, "mykey")
// after
keys, _ := api.Key().List(ctx)
if !keyExists(keys, "mykey") {
    k, err = api.Key().Generate(ctx, "mykey")
}
Defensive patterns

Strategy: validation

Validate before calling

keys, err := api.Key().List(ctx)
if err != nil { return err }
exists := slices.ContainsFunc(keys, func(k coreiface.Key) bool { return k.Name() == name })
if exists {
    return fmt.Errorf("key %q already exists; reuse or remove it first", name)
}

Try / catch

k, err := api.Key().Generate(ctx, name)
if err != nil && strings.Contains(err.Error(), "already exists") {
    return nil // idempotent scripts: treat as success
}

Prevention

When it happens

Trigger: Calling KeyAPI.Generate(ctx, name) (or `ipfs key gen name ...`) with a name that already exists in the repo keystore.

Common situations: 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.

Related errors


AI-assisted analysis of ipfs/kubo@329838acdf (2026-09-03). Data as JSON: /api/errors/3fec3a78e94681c8. Report an issue: GitHub.