Skip to content

Instantly share code, notes, and snippets.

@sedeh
Forked from maurostorch/remove_events_rules.sh
Created January 18, 2022 03:18
Show Gist options
  • Save sedeh/08709636e0c889a8b7d289fc5329aec3 to your computer and use it in GitHub Desktop.
Save sedeh/08709636e0c889a8b7d289fc5329aec3 to your computer and use it in GitHub Desktop.
Remove CloudWatch Rules with targets
#!/bin/bash
# Requires AWS CLI v2
#Usage ./remove_events_rules.sh <rule name, or partial name>
set -eo pipefail
echo "Searching rules for $1";
rules=$(aws events list-rules |jq -r '.Rules[].Name'|grep $1);
echo "Deleting rules:\n$rules";
read -p "Confirm (y/n)?" -n 1 -r
if [[ ! $REPLY =~ ^[Yy]$ ]]
then
[[ "$0" = "$BASH_SOURCE" ]] && exit 1 || return 1
fi
for r in $rules; do
echo "\nListing existing targets:";
tids=$(aws events list-targets-by-rule --rule $r | jq -r '.Targets[].Id');
echo "ids=$tids, removing... ";
for i in $tids; do
aws events remove-targets --rule "$r" --ids "$i";
done
echo "OK\nDeleting rule...";
aws events delete-rule --name $r;
echo "OK";
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment