How to fix error: context not found in Kubernetes

KubernetesBEGINNERMEDIUMUpdated Dec 5, 2025· 139 views

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
  1. List all available contexts
  2. Check your current context
  3. Verify your kubeconfig file location
  4. View the full kubeconfig structure
kubernetes — error
kubectl config get-contexts
error: 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

  1. The context was deleted using kubectl config delete-context but is still referenced as the current-context
  2. The context was created in one kubeconfig file but kubectl is reading from a different file
  3. The KUBECONFIG environment variable is set to a different path than the one containing your contexts
  4. The kubeconfig file was manually edited or corrupted, removing the context definition
  5. Multiple kubeconfig files exist and the KUBECONFIG variable contains duplicates or conflicting entries
  6. A context was created in a tool (like Kind or Minikube) but kubectl is not pointing to that tool's generated kubeconfig
  7. The kubeconfig file at ~/.kube/config doesn't contain the context and KUBECONFIG is not set

How to fix "error: context not found"

1List all available contexts

See what contexts actually exist in your kubeconfig files:

bash
kubectl config get-contexts

This 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.

2Check your current context

View the context that kubectl is currently trying to use:

bash
kubectl config current-context

If this returns a context name that doesn't appear in the get-contexts output, the current-context is set to a non-existent context.

3Verify your kubeconfig file location

Check if you have a custom KUBECONFIG variable set:

bash
echo $KUBECONFIG

If it's empty, kubectl is using ~/.kube/config. If it's set, verify the file(s) exist:

bash
ls -la $KUBECONFIG

On Linux/Mac, multiple files are colon-delimited:

bash
export KUBECONFIG=/path/to/config1:/path/to/config2
4View the full kubeconfig structure

Inspect the complete kubeconfig to see all clusters, users, and contexts:

bash
kubectl config view

Or to see the flattened version (useful if you have multiple kubeconfig files):

bash
kubectl config view --flatten

Look for a contexts: section listing all contexts. Verify that the context you want to use is defined with a cluster and user.

5Create or switch to an existing context

If the context doesn't exist, create it:

bash
kubectl config set-context my-cluster --cluster=my-cluster --user=my-user --namespace=default

If the context exists but isn't active, switch to it:

bash
kubectl config use-context my-cluster

Verify the switch worked:

bash
kubectl config current-context
6Fix kubeconfig mismatch across files

If you created a context with a specific kubeconfig file using the --kubeconfig flag, ensure kubectl can access it:

bash
export KUBECONFIG=$HOME/.kube/config:/path/to/other/config
kubectl config use-context my-context

To make this permanent, add it to your shell profile (~/.bashrc, ~/.zshrc):

bash
echo 'export KUBECONFIG=$HOME/.kube/config:/path/to/config' >> ~/.bashrc
source ~/.bashrc

Verify the context is now accessible:

bash
kubectl config get-contexts

Advanced 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