Skip to content

Instantly share code, notes, and snippets.

@maurostorch
Created October 9, 2021 01:58
Show Gist options
  • Save maurostorch/0a03da8770f9c798d964cb67befae935 to your computer and use it in GitHub Desktop.
Save maurostorch/0a03da8770f9c798d964cb67befae935 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
@sedeh
Copy link

sedeh commented Jan 18, 2022

Nice tool!

@leiarenee
Copy link

Thanks a lot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment