How to fix error: context not found in Kubernetes
This error occurs when kubectl cannot locate the specified context in your kubeconfig file. It commonly happens when switching between clusters, after deleting a context, or due to kubeconfig file mismatches.
Quick fix
Fixes error: context not found
kubectl config get-contexts- List all available contexts
- Check your current context
- Verify your kubeconfig file location
- View the full kubeconfig structure
kubectl config get-contextserror: context not found
What this error means
The 'context not found' error means that kubectl is looking for a Kubernetes context (a named cluster-user-namespace combination) that doesn't exist in your kubeconfig file. A context is a configuration entry that combines three pieces of information: the cluster API server address, user credentials, and a default namespace. When kubectl tries to use a context—either because you set it as current or explicitly requested it—it searches your kubeconfig file(s) and fails if that context name is not defined. This typically happens when the current-context setting points to a context that has been deleted, when you try to use a context that was created in a different kubeconfig file, or when the KUBECONFIG environment variable isn't pointing to the file(s) containing your contexts. Kubeconfig files are YAML documents that store all your Kubernetes cluster access configurations. If your kubeconfig setup is fragmented across multiple files or if you've recently deleted a context, kubectl may be unable to find the context you're trying to use.
Typical symptoms
- Running kubectl commands returns "error: context not found" when trying to use a specific context
- kubectl config current-context shows a context name that doesn't appear in kubectl config get-contexts output
- After creating a context with --kubeconfig flag, you cannot use it with kubectl config use-context
- Switching from one Kubernetes cluster (like Kind or Minikube) to another suddenly stops working
- kubectl commands work in one terminal session but fail in another after setting a KUBECONFIG variable
- Recently deleted a context and now all kubectl commands fail to connect
Common causes
- The context was deleted using kubectl config delete-context but is still referenced as the current-context
- The context was created in one kubeconfig file but kubectl is reading from a different file
- The KUBECONFIG environment variable is set to a different path than the one containing your contexts
- The kubeconfig file was manually edited or corrupted, removing the context definition
- Multiple kubeconfig files exist and the KUBECONFIG variable contains duplicates or conflicting entries
- A context was created in a tool (like Kind or Minikube) but kubectl is not pointing to that tool's generated kubeconfig
- The kubeconfig file at ~/.kube/config doesn't contain the context and KUBECONFIG is not set
How to fix "error: context not found"
See what contexts actually exist in your kubeconfig files:
kubectl config get-contextsThis outputs all contexts across your kubeconfig file(s) and shows which one is currently active (marked with an asterisk). If the context you need is not in this list, it either doesn't exist or is in a kubeconfig file that kubectl isn't reading.
View the context that kubectl is currently trying to use:
kubectl config current-contextIf this returns a context name that doesn't appear in the get-contexts output, the current-context is set to a non-existent context.
Check if you have a custom KUBECONFIG variable set:
echo $KUBECONFIGIf it's empty, kubectl is using ~/.kube/config. If it's set, verify the file(s) exist:
ls -la $KUBECONFIGOn Linux/Mac, multiple files are colon-delimited:
export KUBECONFIG=/path/to/config1:/path/to/config2Inspect the complete kubeconfig to see all clusters, users, and contexts:
kubectl config viewOr to see the flattened version (useful if you have multiple kubeconfig files):
kubectl config view --flattenLook for a contexts: section listing all contexts. Verify that the context you want to use is defined with a cluster and user.
If the context doesn't exist, create it:
kubectl config set-context my-cluster --cluster=my-cluster --user=my-user --namespace=defaultIf the context exists but isn't active, switch to it:
kubectl config use-context my-clusterVerify the switch worked:
kubectl config current-contextIf you created a context with a specific kubeconfig file using the --kubeconfig flag, ensure kubectl can access it:
export KUBECONFIG=$HOME/.kube/config:/path/to/other/config
kubectl config use-context my-contextTo make this permanent, add it to your shell profile (~/.bashrc, ~/.zshrc):
echo 'export KUBECONFIG=$HOME/.kube/config:/path/to/config' >> ~/.bashrc
source ~/.bashrcVerify the context is now accessible:
kubectl config get-contextsAdvanced notes
Understanding kubeconfig structure is essential for managing multiple Kubernetes clusters. A context is a triple of (cluster, user, namespace), and all three must exist in the kubeconfig for the context to work.
When using tools like Kind or Minikube, they automatically generate kubeconfig entries in their own locations. If you merge multiple kubeconfig files using KUBECONFIG, kubectl applies merge rules: the first file to define a value wins.
For security, only trust kubeconfig files from known sources—a malicious kubeconfig can expose credentials or execute code.
When troubleshooting in CI/CD environments, verify that the kubeconfig is correctly mounted or injected into the build container, as file paths may differ from local development.
Some tools use service account tokens instead of static kubeconfig files; in those cases, the error may indicate the service account lacks necessary RBAC permissions rather than a missing context.
Frequently asked questions
- What does "error: context not found" mean?
- The 'context not found' error means that kubectl is looking for a Kubernetes context (a named cluster-user-namespace combination) that doesn't exist in your kubeconfig file. A context is a configuration entry that combines three pieces of information: the cluster API server address, user credentials, and a default namespace. When kubectl tries to use a context—either because you set it as current or explicitly requested it—it searches your kubeconfig file(s) and fails if that context name is not defined.
- How do I fix "error: context not found"?
- This error occurs when kubectl cannot locate the specified context in your kubeconfig file. It commonly happens when switching between clusters, after deleting a context, or due to kubeconfig file mismatches.
- What causes "error: context not found"?
- The context was deleted using kubectl config delete-context but is still referenced as the current-context The context was created in one kubeconfig file but kubectl is reading from a different file The KUBECONFIG environment variable is set to a different path than the one containing your contexts
- Is the "error: context not found" error serious?
- It is a medium-severity error — more of a blocker than a risk. The steps above resolve it.
Official resources & further reading
- 📚Kubernetes Official Documentation
- 📖Docs: kubeconfig and contexts(Official Docs)
- 💬SO: kubectl context not found(Stack Overflow)
- 📖Docs: kubectl cheatsheet(Official Docs)