<p>I've successfully created a MongoDB StatefulSet with persistentVolumeClaim and have questions.  When I use <code>kubectl delete -f X.yml</code> and <code>kubectl create -f X.yml</code> I lose my database data.  Is it expected? Does anyone know why this happens?  Is it because a new persistentVolumeClaim is made with a fresh volume each time?  If so, is there a way to keep the data persistent between <code>create</code>, <code>delete</code>, and <code>create</code>?</p>
<p>I'm running Kubernetes in Minikube.  Here is the yaml for MongoDB:</p>
<pre><code>apiVersion: apps/v1
kind: StatefulSet
metadata:
  labels:
    app: mongo
  name: mongo
spec:
  serviceName: &quot;mongo&quot;
  podManagementPolicy: &quot;Parallel&quot;
  replicas: 1
  selector:
    matchLabels:
      app: mongo
  template:
    metadata:
      labels:
        app: mongo
    spec:
      containers:
      - image: mongo
        name: mongo
        args: [&quot;--dbpath&quot;,&quot;/data/db&quot;]
        livenessProbe:
          exec:
            command:
              - mongosh
              - --eval
              - &quot;db.adminCommand('ping')&quot;
          initialDelaySeconds: 30
          periodSeconds: 10
          timeoutSeconds: 5
          successThreshold: 1
          failureThreshold: 6
        readinessProbe:
          exec:
            command:
              - mongosh
              - --eval
              - &quot;db.adminCommand('ping')&quot;
          initialDelaySeconds: 30
          periodSeconds: 10
          timeoutSeconds: 5
          successThreshold: 1
          failureThreshold: 6
        env:
        - name: MONGO_INITDB_ROOT_USERNAME
          valueFrom:
            secretKeyRef:
              name: mongo-creds
              key: username
        - name: MONGO_INITDB_ROOT_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mongo-creds
              key: password
        volumeMounts:
        - name: &quot;mongo-data-dir&quot;
          mountPath: &quot;/data/db&quot;
      volumes:
      - name: &quot;mongo-data-dir&quot;
        persistentVolumeClaim:
          claimName: &quot;mongo-data&quot;
---
apiVersion: v1
kind: Service
metadata:
  labels:
    app: mongo
  name: mongo-nodeport-svc
spec:
  ports:
  - port: 27017
    protocol: TCP
    targetPort: 27017
    nodePort: 32000
  selector:
    app: mongo
  type: NodePort
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: mongo-data-pv
spec:
  accessModes:
    - ReadWriteOnce
  capacity:
    storage: 1Gi
  hostPath:
    path: /data/mongo
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mongo-data
spec:
  accessModes:
    - ReadWriteOnce 
  resources:
    requests:
      storage: 1Gi
---
apiVersion: v1
data:
  password: XXX
  username: XXX
kind: Secret
metadata:
  creationTimestamp: null
  name: mongo-creds
</code></pre>

notaorb
<p>If the <code>X.yml</code> contains all the volume specs as well then it's expected your data to not be persisted as the volumes and data are removed. If you try to just delete your statefulset and recreate it with</p>
<pre class="lang-bash prettyprint-override"><code>kubectl delete statefulset mongo
kubectl apply -f X.yml
</code></pre>
<p>and then try to access the data from the volume there should be still there.</p>
