Kustomize patch to replace an item in a list
Codemia Knowledge Hub ·
To replace an item in a list with Kustomize, use a strategic merge patch that repeats the item's merge key (usually name) with the new values. For lists that have no merge key, or when you want to target a position, use a JSON 6902 patch with an array index such as /spec/template/spec/containers/0.
YAML lists have no keys of their own, which is why list items (containers, volumes, env vars) are harder to patch than map fields.
Strategic merge patch: match by name
Strategic merge patches match list items by their merge key. For containers, the merge key is name:
JSON 6902 patch: match by index
For precise control, or when items have no merge key, use JSON 6902 patches:
When the container has an env list, an index addresses one entry. The path must exist, or the build fails with doc is missing path:
Adding items to a list
The - at the end of the path means "append to the array."
Removing items from a list
Replacing environment variables
Env vars also use name as the merge key, so a strategic merge patch can replace individual values:
Inline patches
For small changes, define patches inline in kustomization.yaml:
Copying values with replacements (Kustomize 4.5+)
The replacements field copies values between resources declaratively:
Mistakes that break list patches
- Expecting a strategic merge patch to delete items: if you include a list in a strategic merge patch but leave out existing items, those items stay, because the merge matches by
name. Only a plain (non-strategic) merge patch replaces the entire list. - Wrong array index in JSON 6902: indices are zero-based. Targeting
/containers/1when there is only one container fails withpath not found. Runkustomize buildon the base to check the list structure before patching by index. - Merge key mismatch: containers and volumes merge by
name, ports bycontainerPort. If the key in your patch matches nothing in the base, a new item is added instead of replacing the existing one. - Lists with no merge key: lists like
argsandcommandhave none, so a strategic merge patch replaces the whole list. Use JSON 6902 for item-level control. - Patch order: Kustomize applies patches in the order listed in
kustomization.yaml, and later patches see the result of earlier ones. If two patches touch the same list item, the second works on the already-modified version.
Related reading
- Replication Controller VS Deployment in Kubernetes
- Requeue a kubernetes event in a non-blocking reconcile loop
- Required value must specify a volume type when statically provisioning PV
- Restart container within pod
- Replication Modes Definitions?
- Repository is not signed in docker build
- Restart pods when configmap updates in Kubernetes?
- Restart VMs in scale set in AKS Node Pool
System design interview problems on this topic
- Design a Container Orchestration System: solution and walkthrough
- Design a Code Deployment System: solution and walkthrough
- Design a Task Scheduler: solution and walkthrough
- Design a GPU Cluster Scheduler for ML Training: solution and walkthrough

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.