7

How to delete user account from * cognito * in nodejs.

I'm trying to delete the user from cognito it is not working for me.

AWS config

const AWS = require('aws-sdk'); const cognitoidentityserviceprovider = new AWS.CognitoIdentityServiceProvider();

Pool config

const poolConfig = { UserPoolId: keys.cognito.userPoolId, ClientId: keys.cognito.clientId };

Above configurations comes on top of below delete function.

Delete Function

function deleteUserFunc(req, decodedToken) {
    return new Promise((resolve, reject) => {
        const decodedEmailid = decodedToken.email;
        const decodedSub = decodedToken.sub;
        try {
                const userDetails = { Username: decodedSub, Pool: userPool };
                console.log('DEBUG : ' + JSON.stringify(userDetails));
                const cognitoUser = new AmazonCognitoIdentity.CognitoUser(userDetails);

                // Attempting to delete the user

                cognitoidentityserviceprovider.adminDeleteUser({
                    UserPoolId: keys.cognito.userPoolId,
                    Username: decodedEmailid
                    }, (err, data)=>                              
                        if(err) {
                        return reject({
                            error: err.message
                        });
                        } else {
                        return resolve({
                            error: null
                        });
                        }
                    }).promise().catch(err=>{
                    return reject({
                        error: err.message
                    });
                    });

                console.log('User deleteion status : ' + result);

                return resolve({
                    error: null,
                });
            });
        } catch (err) {
            return reject({
                error: err,
            });
        }
    });
}

I have also tried to delete the user with const userDetails = { Username: decodedEmailId, Pool: userPool } but no use.

Any help will much appreciated.

2 Answers 2

17

Try this:

const AWS = require('aws-sdk');
AWS.config.update({
  accessKeyId: 'access key id',
  secretAccessKey: 'secret access key',
  region: 'region',
});
const cognito = new AWS.CognitoIdentityServiceProvider();

await cognito.adminDeleteUser({
  UserPoolId: 'pool id',
  Username: 'username',
}).promise();

Note that the access token you're using within sdk should have cognito-idp:AdminDeleteUser permission on required pool.

4
  • It will work for you if you provide AWS credentials (region, access key, access key id) to aws-sdk. I can update my snippet to show you how. May 8, 2019 at 8:07
  • I have updated my code according to your solution please check it out
    – Rajath
    May 8, 2019 at 9:12
  • @Rajath I also updated my code, have a look, I think it will give you a hint May 8, 2019 at 12:52
  • For the newer syntax with the latest aws libraries checkout this article. They split up the libraries into separate installs and the syntax has changed. docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/…
    – Nick N
    Aug 5, 2022 at 2:46
3

The original answer by @Vladyslav Usenko helped me implement this newer version using the latest aws libraries.

https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-cognito-identity-provider/index.html

The documentation by AWS is very generic and difficult to figure out a solution, but here's an example that should help others.

const { 
    CognitoIdentityProviderClient,
    AdminDeleteUserCommand,
} = require("@aws-sdk/client-cognito-identity-provider");

const client = new CognitoIdentityProviderClient({ region: 'region-goes-here' });

var adminDeleteUserCommandInput = {
    UserPoolId: 'user-pool-here',
    Username: 'user-name-here' 
};
    
const adminDeleteUserCommandResults = await client.send(new AdminDeleteUserCommand(adminDeleteUserCommandInput));
console.log("adminDeleteUserCommandResults", adminDeleteUserCommandResults);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.