How can we delete all users from a specific user pool in AWS Cognito using AWS CLI?
try with below:
aws cognito-idp list-users --user-pool-id $COGNITO_USER_POOL_ID |
jq -r '.Users | .[] | .Username' |
while read uname1; do
echo "Deleting $uname1";
aws cognito-idp admin-delete-user --user-pool-id $COGNITO_USER_POOL_ID --username $uname1;
done
Here is a bash version based on @ajilpm's batch script:
# deleteAllUsers.sh
COGNITO_USER_POOL_ID=$1
aws cognito-idp list-users --user-pool-id $COGNITO_USER_POOL_ID |
jq -r '.Users | .[] | .Username' |
while read user; do
aws cognito-idp admin-delete-user --user-pool-id $COGNITO_USER_POOL_ID --username $user
echo "$user deleted"
done
You must have jq installed and remember to make the script executable: chmod +x deleteAllUsers.sh.
The user pool id can be provided as a command line argument: ./deleteAllUsers.sh COGNITO_USER_POOL_ID.
I created a script to do it from Windows CMD if you have AWS Cli installed and configured, which will delete all the users page by page, so you need to run it till all users are removed.
You need to have JQ downloaded and its path added to system env path for the following to work.
---delete.bat---
@echo off setlocal
for /f "delims=" %%I in
('aws cognito-idp list-users --user-pool-id $COGNITO_USER_POOL_ID ^|
jq -r ".Users | .[] | .Username"')
do
(aws cognito-idp admin-delete-user --user-pool-id $COGNITO_USER_POOL_ID --username %%I
echo %%I deleted)
---delete.bat---