Patch an existing manifest to delete an object using Kustomize

kustomize delete object manifest patchesStrategicMerge

2 min read | by Jordi Prats

Sometimes we'll need to delete a specific resource from an existing manifest. It can be as simple as moving the resource around, but if we do not control the source manifest it might not be an option: In this scenario we can delete the resource using a patch

Let's assume this is the object we want to delete:

apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  name: deleteme-app
  namespace: argocd
spec:
  project: default
  destination:
    namespace: argocd
    server: 'https://kubernetes.default.svc'

In order to remove it we add a patch as usual:

resources:
- ../../base

patchesStrategicMerge:
- delete-resources.yaml

At the patch we'll need to identify the object as usual, including the $patch: delete action:

$patch: delete
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  name: deleteme-app
  namespace: argocd

Now we can use kustomize build to check that the resulting manifest after applying the patch has the object removed:

$ kustomize build cluster/manifests/overlay/dev/ | grep apiVersion -c
3
$ kustomize build cluster/manifests/overlay/prod/ | grep apiVersion -c
2

Posted on 17/11/2022

Categories