kubectx is a utility to manage and switch between kubectl contexts easily. It provides a faster way to switch between clusters and contexts than using kubectl config use-context.
Basic Usage
List All Contexts
Display all available Kubernetes contexts from your kubeconfig:
Example output:
minikube
gke_project_us-central1_cluster-1
arn:aws:eks:us-west-2:123456789012:cluster/production
The current context is highlighted with a different color.
Switch to a Context
Switch to a specific context by name:
Switch context
Output: Switched to context "minikube".
Verify the switch
kubectl config current-context
Output:
Switch to Previous Context
Quickly switch back to the previous context using the - shorthand:
Example:
# Currently on 'production'
kubectx staging
# Switched to context "staging".
kubectx -
# Switched to context "production".
The - shorthand works like cd - in bash, allowing you to quickly toggle between two contexts.
Context Management
Show Current Context
Display the currently active context:
kubectx -c
# or
kubectx --current
Output:
Rename a Context
Rename an existing context using the NEW_NAME=OLD_NAME syntax:
kubectx < new-nam e > = < old-nam e >
Rename specific context
Rename current context
kubectx prod=gke_project_us-central1_production
The . represents the current context. Using new-name=. renames whatever context is currently active.
Example output:
Context gke_project_us-central1_production renamed to prod.
If the new context name already exists, kubectx will overwrite it with a warning:
context "prod" exists, overwriting it.
Context gke_project_us-central1_production renamed to prod.
Unset Current Context
Remove the current context preference from kubeconfig:
kubectx -u
# or
kubectx --unset
Output:
Active context unset for kubectl.
After unsetting the context, kubectl commands will fail until you set a new context with kubectx <name>.
Delete Context(s)
Delete one or more contexts from your kubeconfig:
kubectx -d < context-nam e > [<context-name>...]
Delete single context
Delete multiple contexts
Delete current context
Example output:
Deleted context old-cluster.
If you delete the currently active context:
You deleted the current context. Use "kubectx" to select a new context.
Deleted context production.
Deleting a context only removes the context entry from your kubeconfig. It does not delete the associated user or cluster entries.
Command Reference
All Commands
Command Description kubectxList all contexts kubectx <NAME>Switch to context NAME kubectx -Switch to previous context kubectx -c, --currentShow current context kubectx <NEW>=<OLD>Rename context OLD to NEW kubectx <NEW>=.Rename current context to NEW kubectx -u, --unsetUnset current context kubectx -d <NAME>...Delete context(s) NAME kubectx -h, --helpShow help message kubectx -V, --versionShow version
Using as kubectl Plugin
kubectx can also be used as a kubectl plugin:
kubectl ctx
kubectl ctx minikube
kubectl ctx -
When installed as a kubectl plugin (as kubectl-ctx), all the same commands work with kubectl ctx instead of kubectx.
Practical Examples
Switching Between Environments
# List contexts to see what's available
kubectx
# Switch to development
kubectx dev-cluster
# Switched to context "dev-cluster".
# Do some work, then switch to staging
kubectx staging-cluster
# Switched to context "staging-cluster".
# Need to check something in dev again
kubectx -
# Switched to context "dev-cluster".
Organizing Context Names
# Rename long AWS EKS context names
kubectx dev=arn:aws:eks:us-west-2:123456789012:cluster/dev
kubectx staging=arn:aws:eks:us-west-2:123456789012:cluster/staging
kubectx prod=arn:aws:eks:us-west-2:123456789012:cluster/production
# Now your context list is cleaner
kubectx
# dev
# staging
# prod
Cleanup Old Contexts
# Remove multiple old clusters at once
kubectx -d old-dev old-staging temp-test-cluster
# Deleted context old-dev.
# Deleted context old-staging.
# Deleted context temp-test-cluster.
Next Steps
Interactive Mode Use fzf for interactive context selection
Shell Completion Set up shell autocompletion for faster workflows