How to clean(delete) unmounted PVCs and PVs?

We don't want to delete PV and PVC as pods reuse them most of the times. However, in the long term, we end up with many PVs' and PVCs' that are not used. How to safely clean?

2 answers

  • answered 2018-11-08 08:02 abinet

    Until you keep pvc your pv will be in Bound state. So you can just go and delete unused pvc with: kubectl -n namespace get pvc -o name | grep myname | xargs kubectl -n namespace delete

  • answered 2018-11-08 19:32 Rico

    If you'd like to remove all the Unbound PVs and PVCs, you can do this:

    First delete the PVCs:

    $ kubectl -n <namespace> get pvc | tail -n +2 | grep -v Bound | \
      awk '{print $1}' | xargs -I{} kubectl -n namespace delete pvc {}
    

    Then just delete the PVs:

    $ kubectl -n <namespace> get pv | tail -n +2 | grep -v Bound | \
      awk '{print $1}' | xargs -I{} kubectl -n namespace delete pv {}