Skip to content

Instantly share code, notes, and snippets.

@ShivamJoker
Created August 24, 2023 09:44
Show Gist options
  • Save ShivamJoker/a18aff352d911b910f81f47802454df5 to your computer and use it in GitHub Desktop.
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
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