Created
August 24, 2023 09:44
-
-
Save ShivamJoker/a18aff352d911b910f81f47802454df5 to your computer and use it in GitHub Desktop.
Use AWS Cognito SDK to delete all the users in a cognito user pool
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
import { | |
CognitoIdentityProviderClient, | |
ListUsersCommand, | |
AdminDeleteUserCommand, | |
} from "@aws-sdk/client-cognito-identity-provider"; | |
const USER_POOL_ID = "your-pool-id"; | |
const client = new CognitoIdentityProviderClient({ region: "us-east-1" }); | |
const listUsersCmd = new ListUsersCommand({ UserPoolId: USER_POOL_ID }); | |
const removeAllUsers = async () => { | |
const userRes = await client.send(listUsersCmd); | |
if (!userRes.Users) { | |
throw Error("No user found"); | |
} | |
const deleteUserPromises = userRes.Users?.map((user) => { | |
const deleteCmd = new AdminDeleteUserCommand({ | |
UserPoolId: USER_POOL_ID, | |
Username: user.Username, | |
}); | |
return client.send(deleteCmd); | |
}); | |
const deleteUsersRes = await Promise.all(deleteUserPromises); | |
console.log(deleteUsersRes); | |
}; | |
removeAllUsers(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment