Created
March 4, 2025 15:25
-
-
Save coltenkrauter/7a237040f74c376b6f1e4ee99ac25e28 to your computer and use it in GitHub Desktop.
Delete Cognito Users
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/zsh | |
# delete-cognito-users.sh | |
# deletes all cognito users with emails starting with 'coltenkrauter' | |
# assumes a single user pool exists | |
logInfo() { | |
echo "[INFO] $1" | |
} | |
logError() { | |
echo "[ERROR] $1" | |
} | |
logInfo 'retrieving user pool id...' | |
userPoolId=$(aws cognito-idp list-user-pools --max-results 60 | jq -r '.UserPools[0].Id') | |
if [[ -z "$userPoolId" ]]; then | |
logError 'no user pool found' | |
else | |
logInfo "using user pool: $userPoolId" | |
logInfo "retrieving users with email starting with 'coltenkrauter'..." | |
users=$(aws cognito-idp list-users --user-pool-id "$userPoolId" \ | |
--filter 'email ^= "coltenkrauter"' | jq -r '.Users[].Username') | |
if [[ -z "$users" ]]; then | |
logInfo 'no matching users found' | |
else | |
echo "$users" | while IFS= read -r user; do | |
if [[ -n "$user" ]]; then | |
logInfo "deleting user: $user" | |
aws cognito-idp admin-delete-user --user-pool-id "$userPoolId" --username "$user" | |
if [[ $? -eq 0 ]]; then | |
logInfo "deleted user: $user" | |
else | |
logError "failed to delete user: $user" | |
fi | |
fi | |
done | |
fi | |
fi | |
logInfo "script completed" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment