<p>I want to remove a few environment variables in a container with kustomize? Is that possible? When I patch, it just adds as you may know.</p>
<p>If it's not possible, can we replace environment variable name, and secret key name/key pair all together?</p>
<pre><code>  containers:
    - name: container1
      env:
      - name: NAMESPACE
        valueFrom:
          secretKeyRef:
            name: x
            key: y
</code></pre>
<p>Any help on this will be appreciated! Thanks!</p>

<p>If you're looking remove that <code>NAMESPACE</code> variable from the manifest, you can use the special <code>$patch: delete</code> directive to do so.</p>
<p>If I start with this Deployment:</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: example
spec:
  template:
    spec:
      containers:
        - name: example
          image: docker.io/traefik/whoami:latest
          env:
            - name: ENV_VAR_1
              valueFrom:
                secretKeyRef:
                  name: someSecret
                  key: someKeyName
            - name: ENV_VAR_2
              value: example-value
</code></pre>
<p>If I write in my <code>kustomization.yaml</code>:</p>
<pre><code>apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- deployment.yaml

patches:
  - patch: |
      apiVersion: apps/v1
      kind: Deployment
      metadata:
        name: example
      spec:
        template:
          spec:
            containers:
              - name: example
                env:
                  - name: ENV_VAR_1
                    $patch: delete
</code></pre>
<p>Then the output of <code>kustomize build</code> is:</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: example
spec:
  template:
    spec:
      containers:
      - env:
        - name: ENV_VAR_2
          value: example-value
        image: docker.io/traefik/whoami:latest
        name: example
</code></pre>
<p>Using a strategic merge patch like this has an advantage over a JSONPatch style patch like Nijat's answer because it doesn't depend on the order in which the environment variables are defined.</p>
