kubectl-edit
Edit Kubernetes resources directly
TLDR
Edit a pod in the default namespace
Edit a deployment in the default namespace
Edit a service in the default namespace
Edit all entries of a given resource in a given namespace
Edit a resource using a specific editor
Edit a resource in JSON format
SYNOPSIS
kubectl edit [-f FILENAME | TYPE [NAME_PREFIX | /NAME | -l label_selector]] [--output[=OUTPUT_FORMAT]] [--all-namespaces] [--overwrite[=true|false]] [--save-config[=true|false]] [options]
PARAMETERS
-A, --all-namespaces=false
If true, search for matching resources across all namespaces
-f, --filename=[]
Filename, directory, or URL to file containing the resource to edit
-l, --selector=""
Selector (label query) to filter resources, supports '=', '==', '!='
-o, --output=""
Output format (yaml, json, etc.); defaults to yaml for editing
--overwrite=true
If true, allow overwriting fields that cannot be merged (default true)
--save-config=false
If true, save field manager information in the resource annotation
-R, --recursive=false
Process the directory in -f recursively
--output-version=""
Output the formatted object with the given group version (default api-version)
--raw=""
Raw URI for direct editing of non-standard resources
DESCRIPTION
The kubectl edit command allows users to directly edit the configuration of a live Kubernetes resource using their preferred text editor. It retrieves the current resource definition in YAML or JSON format, opens it in the editor specified by the $EDITOR environment variable (defaults to vi or vim), and upon saving and exiting, applies the changes to the cluster using a strategic merge patch or server-side apply.
This interactive approach is ideal for quick modifications without needing to fetch, edit, and re-apply manifests manually. It supports most resource types like Pods, Deployments, Services, ConfigMaps, and Secrets. Changes are validated by the API server before application, providing immediate feedback on errors.
kubectl edit handles field management intelligently: added fields are preserved, removed fields trigger deletions, and modified fields are updated. It works across namespaces and supports selectors for batch editing. For complex changes, it's safer than imperative updates, as it shows the full current state.
Common use cases include tweaking resource limits, updating labels, or fixing misconfigurations in production. Always review diffs carefully to avoid unintended disruptions.
CAVEATS
Uses $EDITOR (defaults to vi); ensure it's configured. Strategic merge may not handle all list/map changes perfectly. Concurrent edits can cause conflicts. Not suitable for Custom Resources without defined patch strategy. Always backup or diff before saving.
EXAMPLES
kubectl edit deployment/nginx
kubectl edit -n kube-system configmap/kube-proxy
kubectl edit pod -l app=myapp
View changes first: kubectl diff -f deployment.yaml
EDITOR SETUP
Set $EDITOR=nano or $KUBE_EDITOR=code for VSCode. Use kubectl edit --help for full options.
HISTORY
Introduced in early Kubernetes releases (v1.0+), kubectl edit evolved with patch strategies in v1.7+ (strategic merge) and server-side apply in v1.16+. Maintained as core CLI tool by Kubernetes SIG-CLI.
SEE ALSO
kubectl-get(1), kubectl-apply(1), kubectl-patch(1), kubectl-diff(1)


