<p>I have a Kubernetes project managed by Kustomized (Kubernetes). This project deploys two deployments in the same namespace.</p>
<p>Basically, I have the following directory structure:</p>
<pre><code>kustomize -&gt; app1 -&gt; kustomization.yaml
kustomize -&gt; app1 -&gt; namespace.yaml
kustomize -&gt; app1 -&gt; app1.yaml

kustomize -&gt; app2 -&gt; kustomization.yaml
kustomize -&gt; app2 -&gt; namespace.yaml
kustomize -&gt; app2 -&gt; app2.yaml
</code></pre>
<p>The files <code>namespace.yaml</code> create in both the case the same namespace so that the first application deployed, create the namespace and the second reuse it. Obviously, the problem is when I try to remove only one of these applications:</p>
<pre><code>kubectl delete -k kustomize/app1
</code></pre>
<p>remove both the applications because the namespace is removed and app2 too. An easy solution to this problem is to move <code>namespace.yaml</code> outside the folders and just call it standalone. However, this approach requires user must remember to run:</p>
<pre><code>kubectl apply -f namespace.yaml
</code></pre>
<p>before of:</p>
<pre><code>kubectl apply -k kustomize/app1
kubectl apply -k kustomize/app2
</code></pre>
<p>I know another possible solution is via script. My question is that exists a way to better manage namespace removal with Kustomize so that it is removed only if it is empty.</p>

<p>You can have this directory structure:</p>
<pre><code>kustomize -&gt; ns -&gt; namespace.yaml

kustomize -&gt; app1 -&gt; kustomization.yaml
kustomize -&gt; app1 -&gt; app1.yaml

kustomize -&gt; app2 -&gt; kustomization.yaml
kustomize -&gt; app2 -&gt; app2.yaml
</code></pre>
<p>Also you can add a <code>kustomization.yaml</code> at the root, so that you only need this to apply all:</p>
<pre><code>kubectl -k kustomize/
</code></pre>
<p>That will create the namespace and both apps.</p>
<p>And you can still delete only one app if you want:</p>
<pre><code>kubectl delete -k kustomize/app1
</code></pre>
<p>And since you don't have an <code>namespace.yaml</code> in that directory, it does not delete the namespace.</p>
