Knowledge BaseMetric InsightsSystem AdministrationBulk User Deletion (using Custom Script)

Bulk User Deletion (using Custom Script)

Issue

Is there an automated way to delete a list of users that no longer use Metric Insights?

Resolution

One of the possible ways to set up Bulk User Deletion (e.g. by username) is via Custom Script. 

You will need the following components:

  • A Dataset to extract all the users to be deleted;
  • A Custom Script to execute the deletion;
  • An API Access to be able to launch the Custom Script.

To proceed, follow the steps below:

1. Set up API Access and obtain API Token, using our help documentation:

NB: the generated API Token is valid for around 15 minutes (by default), the exact time when the token expires can be seen on the External Application Edit Page (see the screenshot below). We recommend to generate the token right before the script execution. The token has to be valid during all the process of execution. 

To extend the time the token is valid, go to Admin >> Utilities >> System Config and change the value of API_TOKEN_LIFE_TIME variable (consider that the deletion of one user takes around 1 minute on average).

2. Create a new Dataset, that will fetch IDs of all the users to be deleted:

    1) Set the system database Dashboard DB as Data Source

    2) For the SQL query use the following template (User1 and User2 to be replaced by any number of usernames in single quotes, divided by comma): 

SELECT user_id
FROM user
WHERE username in ('User1', 'User2')

    3) Collect the data for the Dataset;

    4) Memorize the Dataset ID (it is included in the Dataset link (see the screenshot below).

3. To create the Custom Script:

    1) Go to Admin >> Utilities >> Custom Scripts;

    2) Create a New Script;

    3) On the Info tab check the External Application Name and Authentication User, there should be set the ones used in p.1 to generate the API Token;

    4) On the Editor tab paste the following script (replace datasetIdToSet with the Dataset ID from p.2.4 directly in the 2nd line of the script):

customScript.parameters = {
    "datasetId":datasetIdToSet
};

function processData(response){
    if('undefined'!==typeof(response) && response.data && response.data.length>0){
        var data = response.data
            , $t = 0;
        for(var $i in data) if(data.hasOwnProperty($i) && data[$i]['user_id']) {
            customScript.runApiRequest(customScript.homeSite + 'api/user/id/'+data[$i]['user_id'], {"type":"DELETE","async":false});

            var now = new Date().getTime();
            while(new Date().getTime() < now + 60000){ /* do nothing */ }
        }

        customScript.close();
    } else
        customScript.log('no dataset rows');
};

function processError(request,error){
    customScript.result('ajax error');
    customScript.close();
};

customScript.runApiRequest(customScript.homeSite+'api/dataset_data?dataset='+customScript.parameters.datasetId,{"success":processData, "error":processError});
customScript.heartBeatTimeout = 30000000;

    5) Execute the Script.

Note 1

We highly recomment to test the solution first: select 1-2 users in the Dataset from p.2, as User Deletion process above is irreversible.

Note 2

If you are going to use this Script multiple times, you will need to specify the new usernames in the Dataset from p.2 and generate each time a new API token from p.1.