kubernetesnamespaceskubectl

Delete Kubernetes namespace with delay


I have the following issue. We need to implement a command which deletes a Kubernetes namespace but with some delay of 1 hour. I tried with this one but it's not working:

kubectl delete ns $NAMESPACE --grace-period=3600

Do you have any ideas or recommendations how it could be done?


Solution

  • Forced deletions can be potentially disruptive for some workloads and their Pods.So we have grace-period - the time that Kubernetes wait before force delete the resources. By default it set to 30 seconds, but you can specify it with this flag --grace-period. Deleting process start at the moment you run the command.

    For you purposes you can use bash script, or, in my view it's more graceful way, Kubernetes Job

    kube-job.yaml:

    apiVersion: batch/v1
    kind: Job
    metadata:
      name: delete-namespace-job
    spec:
      template:
        spec:
          containers:
          - name: delete-namespace
            image: bitnami/kubectl:latest
            command: ["/bin/sh", "-c"]
            args:
            - |
              sleep 3600;
              kubectl delete ns $NAMESPACE;
            env:
            - name: NAMESPACE
              value: "your-namespace"
          restartPolicy: Never
      backoffLimit: 4
    

    and then apply it:

    kubectl apply -f kube-job.yaml