ahmetb/kubectx · error
no namespace exists with name "%s"
Error message
no namespace exists with name "%s"
What it means
After a successful existence query, if the API reports the namespace does not exist (IsNotFound), switchNamespace refuses to point the context at a nonexistent namespace and throws this error naming the namespace. This protects the kubeconfig from being set to a namespace that will fail every subsequent kubectl call. Thrown at cmd/kubens/switch.go:83.
Source
Thrown at cmd/kubens/switch.go:83
prev, err := f.Load()
if err != nil {
return "", fmt.Errorf("failed to load previous namespace from file: %w", err)
}
if ns == "-" {
if prev == "" {
return "", fmt.Errorf("No previous namespace found for current context (%s)", ctx)
}
ns = prev
}
if !force {
ok, err := namespaceExists(kc, ns)
if err != nil {
return "", fmt.Errorf("failed to query if namespace exists (is cluster accessible?): %w", err)
}
if !ok {
return "", fmt.Errorf("no namespace exists with name \"%s\"", ns)
}
}
if err := kc.SetNamespace(ctx, ns); err != nil {
return "", fmt.Errorf("failed to change to namespace \"%s\": %w", ns, err)
}
if err := kc.Save(); err != nil {
return "", fmt.Errorf("failed to save kubeconfig file: %w", err)
}
if curNS != ns {
if err := f.Save(curNS); err != nil {
return "", fmt.Errorf("failed to save the previous namespace to file: %w", err)
}
}
return ns, nil
}
func namespaceExists(kc *kubeconfig.Kubeconfig, ns string) (bool, error) {View on GitHub (pinned to 12ad6fb22e)
Solutions
- List actual namespaces and pick the right name: kubectl get namespaces (or kubens with no args for the interactive list)
- Check you are on the intended context: kubectl config current-context; the namespace may exist on a different cluster
- Create the namespace if it should exist: kubectl create namespace <name>, then retry kubens
- If you know it does not exist yet and still want to set it, use kubens --force <ns>
Example fix
// before kubens prod # NotFound: actual name is production // after kubens production # exact namespace name
Defensive patterns
Strategy: validation
Validate before calling
ok, err := namespaceExists(kc, targetNS)
if err == nil && !ok {
return fmt.Errorf("refusing to switch: namespace %q does not exist; run `kubectl get ns` to list names", targetNS)
} Try / catch
if err := runKubens(ns); err != nil && strings.Contains(err.Error(), "no namespace exists") {
// prompt or autocorrect: nearest match from `kubectl get ns -o name`
return runKubens(nearestNamespace(ns))
} Prevention
- Tab-complete namespace names from the interactive `kubens` list
- Check `kubectl config current-context` — the namespace must exist on the CURRENT cluster
- Create the namespace first (`kubectl create ns <name>`) before pointing kubeconfig at it
- Watch for CI jobs that delete namespaces you depend on
When it happens
Trigger: namespaceExists returned (false, nil): CoreV1 Namespaces().Get for the target name returned NotFound; or the _MOCK_NAMESPACES test hook returned false for names other than ns1/ns2.
Common situations: Typo in the namespace name; the namespace was deleted by someone else or by a CI cleanup; namespace exists in another cluster/context than the one currently selected; namespace is cluster-scoped absent on managed clusters where it was removed (e.g. kube-system deleted accidentally).
Related errors
- failed to query if namespace exists (is cluster accessible?)
- failed to get current context: %w
- failed to get current namespace: %w
- failed to load previous namespace from file: %w
- No previous namespace found for current context (%s)
AI-assisted analysis of ahmetb/kubectx@12ad6fb22e (2026-09-02).
Data as JSON: /api/errors/0570478f3b4a9b14.
Report an issue: GitHub.