<p>I'm trying to patch a deployment and remove its volumes using <code>patch_namespaced_deployment</code> (<a href="https://github.com/kubernetes-client/python" rel="nofollow noreferrer">https://github.com/kubernetes-client/python</a>) with the following arguments, but it's not working.</p>
<pre><code>patch_namespaced_deployment(
            name=deployment_name,
            namespace='default',
            body={&quot;spec&quot;: {&quot;template&quot;: {
                &quot;spec&quot;: {&quot;volumes&quot;: None,
                &quot;containers&quot;: [{'name': container_name, 'volumeMounts': None}]
                }
            }
            }
            },
            pretty='true'
        )
</code></pre>
<p>How to reproduce it:</p>
<p>Create this file app.yaml</p>
<pre><code>apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: myclaim
spec:
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi
---
apiVersion: v1
kind: PersistentVolume
metadata:
  annotations:
    pv.kubernetes.io/bound-by-controller: &quot;yes&quot;
  finalizers:
  - kubernetes.io/pv-protection
  labels:
    volume: pv0001
  name: pv0001
  resourceVersion: &quot;227035&quot;
  selfLink: /api/v1/persistentvolumes/pv0001
spec:
  accessModes:
  - ReadWriteOnce
  capacity:
    storage: 5Gi
  claimRef:
    apiVersion: v1
    kind: PersistentVolumeClaim
    name: myclaim
    namespace: default
    resourceVersion: &quot;227033&quot;
  hostPath:
    path: /mnt/pv-data/pv0001
    type: &quot;&quot;
  persistentVolumeReclaimPolicy: Recycle
  volumeMode: Filesystem
status:
  phase: Bound
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: pv-deploy
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mypv
  template:
    metadata:
      labels:
        app: mypv
    spec:
      containers:
      - name: shell
        image: centos:7
        command:
        - &quot;bin/bash&quot;
        - &quot;-c&quot;
        - &quot;sleep 10000&quot;
        volumeMounts:
        - name: mypd
          mountPath: &quot;/tmp/persistent&quot;
      volumes:
      - name: mypd
        persistentVolumeClaim:
          claimName: myclaim
</code></pre>
<pre><code>- kubectl apply -f app.yaml

- kubectl describe deployment.apps/pv-deploy (to check the volumeMounts and Volumes)

- kubectl patch deployment.apps/pv-deploy --patch '{&quot;spec&quot;: {&quot;template&quot;: {&quot;spec&quot;: {&quot;volumes&quot;: null, &quot;containers&quot;: [{&quot;name&quot;: &quot;shell&quot;, &quot;volumeMounts&quot;: null}]}}}}'

- kubectl describe deployment.apps/pv-deploy (to check the volumeMounts and Volumes)

- Delete the application now: kubectl delete -f app.yaml

- kubectl create -f app.yaml

- Patch the deployment using the python library function as stated above. The *VolumeMounts* section is removed but the Volumes still exist.
</code></pre>
<p>** EDIT **
Running the <code>kubectl patch</code> command works as expected. But
after executing the Python script and running a <code>describe deployment</code> command, the persistentVolumeClaim is replaced with an emptyDir like this</p>
<pre><code>  Volumes:
   mypd:
    Type:       EmptyDir (a temporary directory that shares a pod's lifetime)
    Medium:     
    SizeLimit:  &lt;unset&gt;
</code></pre>

Mehdi Khlifi
<p>What you're trying to do is called a strategic merge patch (<a href="https://kubernetes.io/docs/tasks/manage-kubernetes-objects/update-api-object-kubectl-patch/" rel="nofollow noreferrer">https://kubernetes.io/docs/tasks/manage-kubernetes-objects/update-api-object-kubectl-patch/</a>). As you can see in the documentation, <strong>With a strategic merge patch, a list is either replaced or merged depending on its patch strategy</strong> so this may be why you're seeing this behavior.</p>
<p>I think you should go with <strong>replace</strong> <a href="https://jamesdefabia.github.io/docs/user-guide/kubectl/kubectl_replace/" rel="nofollow noreferrer">https://jamesdefabia.github.io/docs/user-guide/kubectl/kubectl_replace/</a> and instead of trying to manage a part of your deployment object, replace it with a new one.</p>
