# User Delete in auth0

**URL:** <https://community.auth0.com/t/user-delete-in-auth0/129042>\
**Category:** Get Help\
**Tags:** user-profile, user-management\
**Created:** [March 6, 2024, 9:51am UTC](https://community.auth0.com/t/user-delete-in-auth0/129042 "2024-03-06T09:51:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![i-am-sato](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@i-am-sato](https://community.auth0.com/u/i-am-sato)\
**Post date:** [March 6, 2024, 9:51am UTC](https://community.auth0.com/t/user-delete-in-auth0/129042/1 "2024-03-06T09:51:00Z")

</div>

I want to perform user deletion process in “onExecutePreUserRegistration.”  
I’m having trouble with it.

```auto
const auth0 = require("auth0");
exports.onExecutePreUserRegistration = async (event, api) => {

  const managementClient = auth0.ManagementClient;
  const management = new managementClient({
      domain: event.secrets.domain,
      clientId: event.secrets.clientId,
      clientSecret: event.secrets.clientSecret,
  });

 userId = 'test1234567890';
  management.users.delete({ id: userId }, function (err) {
  	if (err) {
      console.log(err);
    }
  });

};

```

```auto
err
{
  init: { method: 'DELETE', headers: {}, body: undefined, agent: undefined },
  context: { path: '/users/test1234567890', method: 'DELETE' }
}

```

Could you please provide instructions on how to delete a user?

---

<div class="post-metadata">

**Author:** ![marcelina.barycka](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/marcelina.barycka/32/56162_2.png) [@marcelina.barycka](https://community.auth0.com/u/marcelina.barycka)\
**Post date:** [March 7, 2024, 11:21am UTC](https://community.auth0.com/t/user-delete-in-auth0/129042/3 "2024-03-07T11:21:49Z")

</div>

Thank you for posting @i-am-sato !

I’m looking into our documentation on the Pre User Registration flow, and I can see that those Actions run _before_ the user is created on a database. The user doesn’t exist yet so it can not be deleted.

> [@i-am-sato](#):
>
> Could you please provide instructions on how to delete a user?

You can reject the user registration with the `api.access.deny method` by running an action analogically to the one below:

```auto
exports.onExecutePreUserRegistration = async (event, api) => {

  if (event.request.geoip.continentCode === "NA") {

    // localize the error message 
    const LOCALIZED_MESSAGES = {
      en: 'You are not allowed to register.',
      es: 'No tienes permitido registrarte.'
    };

    const userMessage = LOCALIZED_MESSAGES[event.request.language] || LOCALIZED_MESSAGES['en'];
    api.access.deny('no_signups_from_north_america', userMessage);
  }
};

```

Please let me if you have any other questions on this topic!

---

<div class="post-metadata">

**Author:** ![i-am-sato](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@i-am-sato](https://community.auth0.com/u/i-am-sato)\
**Post date:** [March 8, 2024, 1:09am UTC](https://community.auth0.com/t/user-delete-in-auth0/129042/4 "2024-03-08T01:09:03Z")

</div>

Thank you for your response.

I apologize for the lack of clarity in my explanation.

If the same email address as mine is already registered, I would like to delete that user.  
There are other conditions as well, but this is a measure to delete data that has become garbage.

I tried changing the process a bit, but the error still persists.

```auto
const auth0 = require("auth0");
exports.onExecutePreUserRegistration = async (event, api) => {

  const managementClient = auth0.ManagementClient;
  const management = new managementClient({
      domain: event.secrets.domain,
      clientId: event.secrets.clientId,
      clientSecret: event.secrets.clientSecret,
      scope: `delete:users delete:current_user`,
  });
  
  let userId = 'auth0|xxxxxxxxxxxxxxxxxxxx'; // Prepare an ID that exists.
  await await management.users.delete({ id:userId }, function (err) {
    if (err) {
      console.log(err);
    }
  });
};

```

ERROR

```auto
{
  "message": "Insufficient scope, expected any of: delete:users,delete:current_user",
  "name": "ManagementApiError",
  "stack": "ManagementApiError: Insufficient scope, expected any of: delete:users,delete:current_user\n at UsersManager.parseError (/data/layers/layers-sB3e/sB3eAYbpTrt8QuVtIrpeZvW2ltBQ92kMEYe62vu_j-c/node_modules/auth0/dist/cjs/management/management-client.js:33:16)\n at process.processTicksAndRejections (node:internal/process/task_queues:95:5)\n at async UsersManager.request (/data/layers/layers-sB3e/sB3eAYbpTrt8QuVtIrpeZvW2ltBQ92kMEYe62vu_j-c/node_modules/auth0/dist/cjs/lib/runtime.js:119:23)\n at async UsersManager.delete (/data/layers/layers-sB3e/sB3eAYbpTrt8QuVtIrpeZvW2ltBQ92kMEYe62vu_j-c/node_modules/auth0/dist/cjs/management/__generated/managers/users-manager.js:152:26)\n at async exports.onExecutePreUserRegistration (/data/io/node18-actions/d0da7aea-41aa-43bd-a75c-fadde77fdd42/webtask.js:14:9)\n at async /data/io/83d1a7565435f5e12c048147b30043a0d29de496.js:5:923"
}

```

LOG

```auto
{
  init: { method: 'DELETE', headers: {}, body: undefined, agent: undefined },
  context: { path: '/users/auth0%7Cxxxxxxxxxxxxxxxxxxxx', method: 'DELETE' }
}

```

---

<div class="post-metadata">

**Author:** ![i-am-sato](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@i-am-sato](https://community.auth0.com/u/i-am-sato)\
**Post date:** [March 12, 2024, 2:42am UTC](https://community.auth0.com/t/user-delete-in-auth0/129042/5 "2024-03-12T02:42:26Z")

</div>

We thought there was a mistake in the program, but the cause was different.

It was just that the Permissions setting was missing in Create Application.

I added checks to the necessary Permissions and it works as expected.

Thank you very much.

 ![img](https://global.discourse-cdn.com/auth0/original/3X/2/2/2268d5891471e4f516c1ba338a7595e484aa7d1b.png)

---

<div class="post-metadata">

**Author:** ![marcelina.barycka](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/marcelina.barycka/32/56162_2.png) [@marcelina.barycka](https://community.auth0.com/u/marcelina.barycka)\
**Post date:** [March 12, 2024, 8:52am UTC](https://community.auth0.com/t/user-delete-in-auth0/129042/6 "2024-03-12T08:52:48Z")

</div>

Thank you so much for sharing @i-am-sato ! I will go ahead and mark your last reply as a solution.

PS: I just noticed you have two "`await` keywords in your code snippet where you initiate a `delete` method 👍

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [March 26, 2024, 8:53am UTC](https://community.auth0.com/t/user-delete-in-auth0/129042/7 "2024-03-26T08:53:23Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
