How to Delete|Remove Kubernetes Cronjob with Kubectl
Posted April 25, 2024
You have created your Kubernetes Cronjobs and you want to delete or remove them from your cluster? This guide is for you. You will use Kubernetes manifest and Kubectl to delete and remove Cronjobs that you no longer need.
How K8s Creates CronJobs and How to Remove Them
In Kubernetes, Cronjob is created periodically. This means, as time goes your Cronjob Scheduler will be executed and create more jobs
Consider the following examples. I used the watch command to check all available Cronjobs:
kubectl get cronjobs apache-cronjob --watch
You can check a dashboard if you have one:
All these jobs create a new resource every time the Scheduler is executed. Now, Once a job is created, you need to delete it and ensure you have fewer resources consumed. Any unneeded resource consumption should be avoided
Now, the question is, how do you delete all these unwanted jobs and still ensure your Cronjob works as expected? Well, let’s learn all the strategies we can use to Delete|Remove Kubernetes Cronjob with Kubectl.
Deleting Entire Kubernetes Cronjob with Kubectl
You can use Kubectl and run a Cronjob delete command. To delete an entire Kubernetes CronJob using the kubectl
cronjob resource type.
kubectl delete cronjob <cronjob_name>
Related: Easily Create Kubernetes k8s CronJob|CronTab with Kubectl
In this case, you might need to first check the name of your cronjob resources with the get command:
kubectl get cronjobs
Let’s say I want to cronjob-container
, the Kubectl delete command will be:
kubectl delete cronjob cronjob-container
# cronjob.batch "cronjob-container" deleted
This command will delete the CronJob and any associated resources, such as Jobs created by the CronJob. At the same time, you can use the --force
flag to remove the entire CronJob resources:
kubectl delete cronjob <cronjob_name> --force
The kubectl delete cronjob Command challenge
The kubectl delete cronjob
command works fine. However, this option will only work manually once the command is executed. At the same time, the whole Cronjob will get deleted and you will need to recreate them if needed.
This means kubectl delete cronjob
can’t be used if you want to delete and avoid accidentally deleting the wrong resource.
The following sections discuss the right ways to manage how Kubernetes self-deletes CronJobs that are not needed.
Deleting CronJobs using Jobs History Limits
Let’s assume you only want to keep several Jobs in your cluster. You will set Jobs History Limits. Jobs History Limits manage the number of historical Active Job records stored in your Kubernetes cluster.
This means Kubernetes will always maintain a given Job History Limits, based on your numbers. A Job can only finish successfully or fail. Jobs History Limits will use two arguments to manage these numbers:
successfulJobsHistoryLimit
sets the maximum number of successful job completions to retain in the history of the CronJob.failedJobsHistoryLimit
with the maximum number of failed job completions to retain in the history of the CronJob.
successfulJobsHistoryLimit and failedJobsHistoryLimit will control how many completed and failed Jobs are retained in the history of the CronJob. Once these limits are reached, older Jobs are automatically deleted to avoid clutter and conserve resources.
You will add:
spec.successfulJobsHistoryLimit
where its default value is 3. Setting this field to 0 will not keep any successful jobs.
.spec.failedJobsHistoryLimit
has a default value is 1. Setting this field to 0 will not retain any historical records of failed job completions.
apiVersion: batch/v1
kind: CronJob
metadata:
name: alpine-cronjob
spec:
schedule: "*/1 * * * *"
successfulJobsHistoryLimit: 3
failedJobsHistoryLimit: 1
jobTemplate:
spec:
template:
# The Rest of the manifest code
Using ttlSecondsAfterFinished to Remove CronJobs
Kubernetes uses ttlSecondsAfterFinished
as TTL (Time-To-Live) duration. TTL is the Time-To-Live duration for completed Jobs created by a CronJob. This means ttlSecondsAfterFinished will have an amount of time to retain a Job after it has been completed before it is automatically deleted.
You don’t need a command here. Kubernetes will let you add this to your manifest files. When a Job created by the CronJob finishes successfully or fails, it enters a completed state. This way, ttlSecondsAfterFinished will be executed within your cluster and CronJobs automatically deleted.
You’ll add the ttlSecondsAfterFinished
field to your CronJob manifest as follows:
apiVersion: batch/v1
kind: Job
metadata:
name: alpine-cronjob
spec:
ttlSecondsAfterFinished: 3600 # TTL set to 1 hour (3600 seconds)
jobTemplate:
spec:
template:
# The Rest of the manifest code
metadata:
labels:
app: alpine-cronjob
spec:
containers:
- name: alpine-container
image: alpine:latest
command: ["echo", "Hello from Alpine!"]
restartPolicy: OnFailure
This example will use ttlSecondsAfterFinished: 3600
. Here 3600 is duration in seconds. This way, completed Jobs are retained for 3600 seconds (1 hour) after they finish. This time will vary based on your Kubernetes k8s Cronjob Schedule Expression.
You have the Automatic Deletion added and ready. Once a completed Job reaches the end of the TTL duration, Kubernetes automatically deletes it from the cluster.
Conclusion
Are you now able to delete or remove your Kubernetes CronJobs? I hope this guide helped you Delete|Remove Kubernetes Cronjob with Kubectl.