{"record":{"id":"0570478f3b4a9b14","repo":"ahmetb/kubectx","slug":"no-namespace-exists-with-name-s","errorCode":null,"errorMessage":"no namespace exists with name \"%s\"","messagePattern":"no namespace exists with name \"(.+?)\"","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cmd/kubens/switch.go","lineNumber":83,"sourceCode":"\tprev, err := f.Load()\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"failed to load previous namespace from file: %w\", err)\n\t}\n\n\tif ns == \"-\" {\n\t\tif prev == \"\" {\n\t\t\treturn \"\", fmt.Errorf(\"No previous namespace found for current context (%s)\", ctx)\n\t\t}\n\t\tns = prev\n\t}\n\n\tif !force {\n\t\tok, err := namespaceExists(kc, ns)\n\t\tif err != nil {\n\t\t\treturn \"\", fmt.Errorf(\"failed to query if namespace exists (is cluster accessible?): %w\", err)\n\t\t}\n\t\tif !ok {\n\t\t\treturn \"\", fmt.Errorf(\"no namespace exists with name \\\"%s\\\"\", ns)\n\t\t}\n\t}\n\n\tif err := kc.SetNamespace(ctx, ns); err != nil {\n\t\treturn \"\", fmt.Errorf(\"failed to change to namespace \\\"%s\\\": %w\", ns, err)\n\t}\n\tif err := kc.Save(); err != nil {\n\t\treturn \"\", fmt.Errorf(\"failed to save kubeconfig file: %w\", err)\n\t}\n\tif curNS != ns {\n\t\tif err := f.Save(curNS); err != nil {\n\t\t\treturn \"\", fmt.Errorf(\"failed to save the previous namespace to file: %w\", err)\n\t\t}\n\t}\n\treturn ns, nil\n}\n\nfunc namespaceExists(kc *kubeconfig.Kubeconfig, ns string) (bool, error) {","sourceCodeStart":65,"sourceCodeEnd":101,"githubUrl":"https://github.com/ahmetb/kubectx/blob/12ad6fb22e8c546ee2b54e7de38aa51c906832f7/cmd/kubens/switch.go#L65-L101","documentation":"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.","triggerScenarios":"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.","commonSituations":"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).","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>"],"exampleFix":"// before\nkubens prod   # NotFound: actual name is production\n// after\nkubens production   # exact namespace name","handlingStrategy":"validation","validationCode":"ok, err := namespaceExists(kc, targetNS)\nif err == nil && !ok {\n    return fmt.Errorf(\"refusing to switch: namespace %q does not exist; run `kubectl get ns` to list names\", targetNS)\n}","typeGuard":null,"tryCatchPattern":"if err := runKubens(ns); err != nil && strings.Contains(err.Error(), \"no namespace exists\") {\n    // prompt or autocorrect: nearest match from `kubectl get ns -o name`\n    return runKubens(nearestNamespace(ns))\n}","preventionTips":["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"],"tags":["kubernetes-api","kubens","validation"],"backgroundTag":"namespace-not-found","analyzedSha":"12ad6fb22e8c546ee2b54e7de38aa51c906832f7","analyzedAt":"2026-09-02T12:23:10.107Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T16:17:10.729Z"}