How to delete all schedulers from the Amazon EventBridge scheduler
Do you come across the same situation where you have to delete all the schedulers from the Amazon Eventbridge Scheduler? Some of us have been there. Well, it turns out that there is no way in the AWS console to delete all schedules other than deleting them one by one. This approach is boring, time-consuming, and a tedious task. This becomes more difficult when you have thousands of schedules. Recently, I have had to delete all the schedules from the Amazon Eventbridge Scheduler which we are using for development purposes. If you want to do the same then you have to be very very careful as this type of action can result in loss of information.
As I was looking for a solution to this task in one go through avoiding the one-by-one deletion, I was looking for an option in the AWS CLI. My initial thinking was that there might be some kind of command for schedulers that would allow me to do this. because in my experience it is usually the case that the CLI sometimes offers more than the AWS console. This is not the case for every service in aws but this is definitely true for some cases. For example, if we want to attach an email sender lambda to the cognito user pool there is no direct way to do so in the console but we can do the job using the AWS CLI.
Enough of the chit-chat, now let us dive into the action.
Prerequisites
Jq
AWS CLI configured with AWS credentials
AWS CLI version > 2.7 (latest version of CLI includes scheduler command)
Amazon Eventbridge Schedulers
Shell Script
# delete-all-scheduler.sh
SCHEDULER_GROUP="develop"
aws scheduler list-schedules --group-name $SCHEDULER_GROUP |
jq -r '.Schedules | .[] | .Name' |
while read schedulename; do
aws scheduler delete-schedule --group-name $SCHEDULER_GROUP --name $schedulename
echo "$schedulename deleted"
done
We need to run the above shell script to delete all schedulers from the Amazon Eventbridge Scheduler. Just create a new file in your desired folder location, and give it a name, in our case we name it delete-all-schedulers.sh
. Also, we need to specify the name of the scheduler group in the script under which all the schedulers are residing. Now enter your scheduler group name.
How to run the script
Enter the following code in the terminal to run the script.
sh delete-all-users.sh
Key points to understand in the script
This shell script uses another command line tool named jq which is a lightweight and flexible command-line JSON processor. Chances are you don’t have it installed in our system or bash. Since I am a long-term Mac user, I use Homebrew to install dependencies and other command line tools. You just need to run the following command in your Mac terminal to install jq if you have Homebrew installed already. Or you can follow this link to install jq with Homebrew.
brew install jq
There are two AWS CLI commands in the shell script that we need to understand. This is to keep ourselves alert to any upcoming ramifications.
aws scheduler list-schedules --group-name $SCHEDULER_GROUP
This command is mainly used for listing all scheduler information against the given scheduler group. Here is a screenshot of the command output.As we can see the "Name" parameter in the output is a unique identifier for all schedules that are given by us. We are using this name parameter in the shell script to delete all schedules.
aws scheduler delete-schedule --group-name $SCHEDULER_GROUP --name $schedulername
This command is used to delete a specific scheduler from the given scheduler group. The "schedulername" part of the command is the unique identifier generated for each scheduler given by the creator as we saw in the previous point.
In the shell script what we are doing is listing all schedules of an Eventbridge Scheduler for a given scheduler group by running the first command and then by using jq we are picking the name object from each listing and running the delete command for each user found in the scheduler list.