<p>I am trying to deploy a helm chart which uses <code>PersistentVolumeClaim</code> and <code>StorageClass</code> to dynamically provision the required sotrage. This works as expected, but I can't find any configuration which allows a workflow like</p>

<pre><code>helm delete xxx

# Make some changes and repackage chart

helm install --replace xxx
</code></pre>

<p>I don't want to run the release constantly, and I want to reuse the storage in deployments in the future.</p>

<p>Setting the storage class to <code>reclaimPolicy: Retain</code> keeps the disks, but helm will delete the PVC and orphan them. Annotating the PVC's so that helm does not delete them fixes this problem, but then running install causes the error</p>

<pre><code>Error: release xxx failed: persistentvolumeclaims "xxx-xxx-storage" already exists
</code></pre>

<p>I think I have misunderstood something fundamental to managing releases in helm. Perhaps the volumes should not be created in the chart at all.</p>

<p><a href="https://kubernetes.io/docs/concepts/storage/persistent-volumes/#persistentvolumeclaims" rel="nofollow noreferrer">PersistenVolumeClain</a> creating just a mapping between your actual <a href="https://kubernetes.io/docs/concepts/storage/persistent-volumes/" rel="nofollow noreferrer">PersistentVolume</a> and your pod.</p>
<p>Using <code>&quot;helm.sh/resource-policy&quot;: keep</code> annotation for PV is not the best idea, because of that remark in a <a href="https://github.com/kubernetes/helm/blob/master/docs/charts_tips_and_tricks.md#tell-tiller-not-to-delete-a-resource" rel="nofollow noreferrer">documentation</a>:</p>
<blockquote>
<p>The annotation &quot;helm.sh/resource-policy&quot;: keep instructs Tiller to skip this resource during a helm delete operation. However, this resource becomes orphaned. Helm will no longer manage it in any way. This can lead to problems if using helm install --replace on a release that has already been deleted, but has kept resources.</p>
</blockquote>
<p>If you will create a PV manually after you will delete your release, Helm will remove PVC, which will be marked as &quot;Available&quot; and on next deployment, it will reuse it.  Actually, you don't need to keep your PVC in the cluster to keep your data. But, for making it always using the same PV, you need to use <a href="https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/" rel="nofollow noreferrer">labels and selectors</a>.</p>
<p>For keep and reuse volumes you can:</p>
<ol>
<li>Create PersistenVolume with the label, as an example, <code>for_app=my-app</code> and set &quot;Retain&quot; policy for that volume like this:</li>
</ol>
<pre><code>apiVersion: v1
kind: PersistentVolume
metadata:
  name: myappvolume
  namespace: my-app
  labels:
    for_app: my-app
spec:
  persistentVolumeReclaimPolicy: Retain
  capacity:
    storage: 5Gi
  accessModes:
    - ReadWriteOnce
</code></pre>
<ol start="2">
<li>Modify your PersistenVolumeClaim configuration in Helm. You need to add a selector for using only PersistenVolumes with a label <code>for_app=my-app</code>.</li>
</ol>
<pre><code>apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: myappvolumeclaim
  namespace: my-app
spec:
  selector:
    matchLabels:
      for_app: my-app
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 5Gi
</code></pre>
<p>So, now your application will use the same volume each time when it started.</p>
<p>But, please keep in mind, you may need to use selectors for other apps in the same namespace for preventing using your PV by them.</p>
