Delete Script Templates

The Delete script implements the defined function to delete a specified user from an external database. We recommend naming this function deleteUser.

The script is only used in a legacy authentication scenario, and is required if you want to delete a user from Auth0 and your external database in the same operation.

DeleteUser function

The deleteUser function should:

  • Remove the specified user from the external database.

  • Return a null value if the deletion succeeded.

  • Return an error if the deletion failed.

Definition

The deleteUser function accepts two parameters and returns a callback function:

deleteUser(id, callback): function

Was this helpful?

/

Parameter Type Description
id String User’s identity user_id. Does not start with auth0.
callback Function Used to pass error or profile data through the pipeline.

Example

This is a pseudo-JavaScript example of how you could implement the deleteUser function. For language-specific examples, read Language-specific script examples.

function deleteUser(id, callback) {
  // Send user identifier to external database API
  let options = {
    url: "https://example.com/api/deleteUser",
    body: {
      id: id
    }
  };

  send(options, (err, profileData) => {
    // Return error in callback if deletion failed
    if (err) {
      return callback(new Error("My custom error message."));
    }

    // Return null value in callback if deletion succeeded
    return callback(null);
  });
}

Was this helpful?

/

Callback function

The callback function accepts one parameter and returns a function.

Definition

callback(error): function

Was this helpful?

/

Parameter Type Required Description
error Object Required Contains error data.

Return a success

If the user was successfully deleted in your external database, return the callback function, and pass a null value for the error parameter.

Example

return callback(null);

Was this helpful?

/

Return an error

If an error occurs, return the callback function, and pass relevant error information to the error parameter.

Example

return callback(new Error("My custom error message."));

Was this helpful?

/

Language-specific script examples

Auth0 provides sample scripts for use with the following languages/technologies:

JavaScript

function remove (id, callback) {
  // This script remove a user from your existing database.
  // It is executed whenever a user is deleted from the Management API or Auth0 dashboard.
  //
  // There are two ways that this script can finish:
  // 1. The user was removed successfully:
  //     callback(null);
  // 2. Something went wrong while trying to reach your database:
  //     callback(new Error("my error message"));
  var msg = "Please implement the Delete script for this database " +
       "connection at https://manage.auth0.com/#/connections/database";
  return callback(new Error(msg));
}

Was this helpful?

/

ASP.NET Membership Provider (MVC3 - Universal Providers)

function remove(id, callback) {
  const sqlserver = require('tedious@1.11.0');
  const Connection = sqlserver.Connection;
  const Request = sqlserver.Request;
  const TYPES = sqlserver.TYPES;
  const connection = new Connection({
    userName: 'the username',
    password: 'the password',
    server: 'the server',
    options: {
      database: 'the db name',
      encrypt: true,
      // Required to retrieve userId needed for Membership entity creation
      rowCollectionOnRequestCompletion: true
    }
  });
  connection.on('debug', function(text) {
    // if you have connection issues, uncomment this to get more detailed info
    // console.log(text);
  }).on('errorMessage', function(text) {
    // this will show any errors when connecting to the SQL database or with the SQL statements
    console.log(JSON.stringify(text));
  });
  connection.on('connect', function(err) {
    if (err) return callback(err);
    executeDelete(['Memberships', 'Users'], function(err) {
      if (err) return callback(err);
      callback(null);
    });
  });
  function executeDelete(tables, callback) {
    const query = tables.map(function(table) {
      return 'DELETE FROM ' + table + ' WHERE UserId = @UserId';
    }).join(';');
    const request = new Request(query, function(err) {
      if (err) return callback(err);
      callback(null);
    });
    request.addParameter('UserId', TYPES.VarChar, id);
    connection.execSql(request);
  }
}

Was this helpful?

/

ASP.NET Membership Provider (MVC4 - Simple Membership)

function remove(id, callback) {
  const sqlserver = require('tedious@1.11.0');
  const Connection = sqlserver.Connection;
  const Request = sqlserver.Request;
  const TYPES = sqlserver.TYPES;
  const connection = new Connection({
    userName: 'the username',
    password: 'the password',
    server: 'the server',
    options: {
      database: 'the db name',
      encrypt: true,
      // Required to retrieve userId needed for Membership entity creation
      rowCollectionOnRequestCompletion: true
    }
  });
  connection.on('debug', function (text) {
    // if you have connection issues, uncomment this to get more detailed info
    // console.log(text);
  }).on('errorMessage', function (text) {
    // this will show any errors when connecting to the SQL database or with the SQL statements
    console.log(JSON.stringify(text));
  });
  connection.on('connect', function (err) {
    if (err) return callback(err);
    executeDelete(['webpages_Membership', 'UserProfile'], function (err) {
      if (err) return callback(err);
      callback(null);
    });
  });
  function executeDelete(tables, callback) {
    const query = tables.map(function (table) {
      return 'DELETE FROM ' + table + ' WHERE UserId = @UserId';
    }).join(';');
    const request = new Request(query, function (err) {
      if (err) return callback(err);
      callback(null);
    });
    request.addParameter('UserId', TYPES.VarChar, id);
    connection.execSql(request);
  }
}

Was this helpful?

/

MongoDB

function remove(id, callback) {
  const MongoClient = require('mongodb@3.1.4').MongoClient;
  const client = new MongoClient('mongodb://user:pass@mymongoserver.com');
  client.connect(function (err) {
    if (err) return callback(err);
    const db = client.db('db-name');
    const users = db.collection('users');
    users.remove({ _id: id }, function (err) {
      client.close();
      if (err) return callback(err);
      callback(null);
    });
  });
}

Was this helpful?

/

MySQL

function remove(id, callback) {
  const mysql = require('mysql');
  const connection = mysql({
    host: 'localhost',
    user: 'me',
    password: 'secret',
    database: 'mydb'
  });
  connection.connect();
  const query = 'DELETE FROM users WHERE id = ?';
  connection.query(query, [ id ], function(err) {
    if (err) return callback(err);
    callback(null);
  });
}

Was this helpful?

/

PostgreSQL

function remove(id, callback) {
  //this example uses the "pg" library
  //more info here: https://github.com/brianc/node-postgres
  const postgres = require('pg');
  const conString = 'postgres://user:pass@localhost/mydb';
  postgres.connect(conString, function (err, client, done) {
    if (err) return callback(err);
    const query = 'DELETE FROM users WHERE id = $1';
    client.query(query, [id], function (err) {
      // NOTE: always call `done()` here to close
      // the connection to the database
      done();
      return callback(err);
    });
  });
}

Was this helpful?

/

SQL Server

function remove(id, callback) {
  // this example uses the "tedious" library
  // more info here: http://pekim.github.io/tedious/index.html
  const sqlserver = require('tedious@1.11.0');
  const Connection = sqlserver.Connection;
  const Request = sqlserver.Request;
  const TYPES = sqlserver.TYPES;
  const connection = new Connection({
    userName:  'test',
    password:  'test',
    server:    'localhost',
    options:  {
      database: 'mydb'
    }
  });
  const query = 'DELETE FROM dbo.Users WHERE id = @UserId';
  connection.on('debug', function (text) {
    console.log(text);
  }).on('errorMessage', function (text) {
    console.log(JSON.stringify(text, null, 2));
  }).on('infoMessage', function (text) {
    console.log(JSON.stringify(text, null, 2));
  });
  connection.on('connect', function (err) {
    if (err) return callback(err);
    const request = new Request(query, function (err) {
      if (err) return callback(err);
      callback(null);
    });
    request.addParameter('UserId', TYPES.VarChar, id);
    connection.execSql(request);
  });
}

Was this helpful?

/

Windows Azure SQL Database

function remove (id, callback) {
  // this example uses the "tedious" library
  // more info here: http://pekim.github.io/tedious/index.html
  var Connection = require('tedious@1.11.0').Connection;
  var Request = require('tedious@1.11.0').Request;
  var TYPES = require('tedious@1.11.0').TYPES;
  var connection = new Connection({
    userName: 'your-user@your-server-id.database.windows.net',
    password: 'the-password',
    server: 'your-server-id.database.windows.net',
    options: {
      database: 'mydb',
      encrypt: true
    }
  });
  connection.on('debug', function (text) {
    console.log(text);
  }).on('errorMessage', function (text) {
    console.log(JSON.stringify(text, null, 2));
  }).on('infoMessage', function (text) {
    console.log(JSON.stringify(text, null, 2));
  });
  connection.on('connect', function (err) {
    if (err) { return callback(err); }
    var query = 'DELETE FROM users WHERE id = @UserId';
    var request = new Request(query, function (err) {
      if (err) { return callback(err); }
      callback(null);
    });
    request.addParameter('UserId', TYPES.VarChar, id);
    connection.execSql(request);
  });
}

Was this helpful?

/

Learn more