Kubernetes Delete Persistent Voulmes Created by hostPath

9/20/2019

I created a PV and a PVC on docker-desktop and even after removing the pv and pvc the file still remains. When I re-create it, it attaches the same mysql database to new pods. How do you manually delete the files created by the hostPath? I suppose one way is to just reset Kubernetes in the preferences but there has to be another less nuclear option.

apiVersion: v1
kind: PersistentVolume
metadata:
  name: mysql-pv-volume
  labels:
    type: local
spec:
  storageClassName: manual
  capacity:
    storage: 20Gi
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Delete
  hostPath:
    path: "/mnt/data"
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysql-pv-claim2
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 20Gi
-- duffney
kubernetes
persistent-volume-claims
persistent-volumes

3 Answers

9/20/2019

hostPath volumes are simply folders on one of your node's filesystem (in this case /mnt/data). All you need to do is delete that folder from the node that hosted the volume.

-- Alassane Ndiaye
Source: StackOverflow

9/23/2019

If you defined any node affinity to pod that you need to check. Then find out node where that pod is schedule. Delete PVC an PV Then delete data from /mnt/data directory.

kubectl get pod -o wide | grep <pod_name>

Here you will get on which node it is scheduled.

kubectl delete deploy or statefulset <deploy_name>

kubectl get pv,pvc

kubectl delete pv <pv_name>

kubectl delete pvc <pvc_name>

Now go on that node and delete that data from /mnt/data

One more way to do it you can define persistentVolumeReclaimPolicy to retain or delete

-- Sachin Arote
Source: StackOverflow

9/21/2019

According to the docs, "...Recycle reclaim policy performs a basic scrub (rm -rf /thevolume/*) on the volume and makes it available again for a new claim". Also, "...Currently, only NFS and HostPath support recycling". So, try changing

persistentVolumeReclaimPolicy: Delete

to

persistentVolumeReclaimPolicy: Recycle
-- apisim
Source: StackOverflow