Auth0 Home Blog Docs

I can not delete Users using Custom Database

custom-database
delete-user

#1

Hello!

I am trying to delete a user using the the templates of custom database -> delete section.
When I do the test always the result is "The user was successfully deleted, but I can see in my database the user is still there… when I delete the user from the dashboard the user is deleted in the user collection in the dashboard but not in my database.
Maybe I am missing something.

I appreciate your help.


#2

This indicates an issue with your Delete script. You will need to make sure that your script actually deletes the user from your custom database. An example for Mongo:

...
users.remove({ _id: id }, function (err) {
  if (err) return callback(err);
  callback(null);
});

To debug, you can use console.log() statements in your script, and the Realtime Webtask Logs extension to review the logs.


#3

There’s one problem though - usually _id of doc in MongoDB is stored as ObjectId. Hence there’s a difference between:
{_id: ‘123123123123’}
and
{_id: ObjectId(‘123123123123’)}
The first one won’t do nothing but the second one will actually delete that user.

Is there a way to access ObjectId constructor inside of custom database scripts?

Thank you for your answer!


#4