Skip to content

Instantly share code, notes, and snippets.

@dehli
Last active October 2, 2019 19:57
Show Gist options
  • Save dehli/15bacb87d57ac2b5df5e69d641899680 to your computer and use it in GitHub Desktop.
Save dehli/15bacb87d57ac2b5df5e69d641899680 to your computer and use it in GitHub Desktop.
clear_cognito_pool
#!/usr/bin/env node
const { USER_POOL_ID } = process.env;
if (!USER_POOL_ID) throw new Error("Missing USER_POOL_ID");
const AWS = require("aws-sdk");
const cognito = new AWS.CognitoIdentityServiceProvider();
const attribute => ({ Attributes }, name) =>
Attributes.filter(a => a.Name === name)[0].Value;
const shouldDelete = user =>
attribute(user, "email").indexOf("dev+") > -1;
(async function() {
while (true) {
const { Users } = await cognito.listUsers({ UserPoolId: USER_POOL_ID }).promise();
let deletedUsers = 0;
for (const user of Users) {
if (shouldDelete(user)) {
await cognito.adminDeleteUser({
UserPoolId: USER_POOL_ID,
Username: user.Username
}).promise();
deletedUsers++;
}
}
if (deletedUsers === 0) break;
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment