<p>I am using Kustomize to manage my Kubernetes project with a StetefulSet that deploys a PostgreSQL cluster with three pods. I am working on Vagrant/VirtualBox so no dynamic provisioning of PV exists. For this reason, I have my own <code>pv.yaml</code> containing the manifest to deploy these 3 PVs.</p>
<p>Then I have a <code>kustomization.yaml</code> file like this:</p>
<pre><code>namespace: ibm-cfdb

bases:
- ../../bases

resources:
- pv.yaml
</code></pre>
<p>the folder <code>../../bases</code> contains the file to deploy the StatefulSet. When I run:
<code>kubectl apply -k kustomize/</code> everything is correctly deployed. PVs are created before the StetefulSet that contains a <code>volumeClaimTemplates</code> that declare the Claim for these PVs.</p>
<p>The problem is that when I try to remove the deployment with the command:
<code>kubectl delete -k kustomize/</code> the removal of PV is executed (it seems I don't have control about the order). I suppose these PVs cannot be deleted because Claims use them. Then the StatefulSet removal stuck.</p>
<p>What is the best approach to manage PV static provisioning with Kustomize?</p>

<p>You encountered an interesting problem regarding StatefulSet and PVC removal. There is a <a href="https://github.com/kubernetes/kubernetes/issues/55045" rel="nofollow noreferrer">discussion</a> whether PVCs created by the StatefulSet should be deleted when deleting the corresponding StatefulSet. We recently received <a href="https://github.com/kubernetes/kubernetes/issues/55045#issuecomment-824937931" rel="nofollow noreferrer">information</a> that the feature to autodelete the PVCs created by StatefulSet will probably be available in the <a href="https://github.com/kubernetes/kubernetes/issues/55045#issuecomment-884298382" rel="nofollow noreferrer">1.23 release</a>. According to the <a href="https://github.com/kubernetes/enhancements/tree/master/keps/sig-apps/1847-autoremove-statefulset-pvcs#changes-required" rel="nofollow noreferrer">feature documentation</a>, this will allow us to specifiy if the VolumeClaimTemplate PVCs will be deleted after deleting their StatefulSet.
I suspect that with this feature it'll be easy to delete your StatefulSet along with PVC and PV.</p>
<p>For now, you can consider moving the file with the PV to another directory and manage it separately.
However, I will propose another solution which is kind of a workaround but you may be interested.</p>
<p>Basically, we can use the <code>-o</code> flag with the <code>kustomize build</code> command. This creates one file per resource, which gives us more control over resources creation.</p>
<hr />
<p>I will give you an example to illustrate how this can work.</p>
<p>Suppose I have a similar environment to you:</p>
<pre><code>$ tree
.
├── base
│   ├── kustomization.yaml
│   └── statefulset.yaml
└── overlays
    └── dev
        ├── kustomization.yaml
        └── pv.yaml

$ cat overlays/dev/kustomization.yaml
bases:
  - ../../base

resources:
- pv.yaml
</code></pre>
<p>Now let's create a directory where our manifest files generated by <code>kustomize</code> will be stored:</p>
<pre><code>$ mkdir generated_manifests
</code></pre>
<p>Then we can check if the command <code>kustomize build overlays/dev -o generated_manifests</code> works as expected. First we'll apply the generated manifests (it'll create the <code>web</code> StatefulSet and <code>pv0003</code> PersistentVolume):</p>
<pre><code>$ kustomize build overlays/dev -o generated_manifests &amp;&amp; kubectl apply -Rf generated_manifests/
statefulset.apps/web created
persistentvolume/pv0003 created
</code></pre>
<p>As you can see, the appropriate manifest files have been created in the <code>generated_manifests</code> directory:</p>
<pre><code>$ ls generated_manifests/
apps_v1_statefulset_web.yaml  v1_persistentvolume_pv0003.yaml
</code></pre>
<p>Finally, we can try to delete only the <code>web</code> StatefulSet:</p>
<pre><code>$ kustomize build overlays/dev -o generated_manifests &amp;&amp; kubectl delete -f generated_manifests/apps_v1_statefulset_web.yaml
statefulset.apps &quot;web&quot; deleted
</code></pre>
<hr />
<p>I would also like to mention that <code>kustomize</code> has a feature like &quot;ignore pv.yaml&quot; but it will also be used when creating resources, not just when removing. This is known as a delete patch and a good example can be found <a href="https://stackoverflow.com/a/66074466/14801225">here</a>.</p>
