Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to bulk delete user's via background script after cloning

attanhes
Tera Guru

I'm trying to bulk delete 'Inactive' users from the sys_user table using a background script after a clone, but it's taking a long time to delete even 10 users records. After a while, the system becomes unresponsive.

var gr = new GlideRecord('sys_user');
gr.addQuery('active', false);
gr.query();

var count = 0;
while (gr.next() && count < 10) {
    gr.deleteRecord();
    count++;
}
gs.print('Deleted ' + count + ' inactive users.');

 

Is there any better way to doing the above script ?

I tried multiple record deletion as well, but same result.

var gr = new GlideRecord(sys_user);
gr.addQuery('active', false);
gr.setLimit(10); //limit only first  2 record 
gr.query();
gr.deleteMultiple();

I noticed there's a 'Clone Cleanup Script', but I've never used it before and I'm not sure how to test it either.

1 ACCEPTED SOLUTION

J Siva
Kilo Patron
Kilo Patron

Hi @attanhes 
May I ask why you want to delete the records? It's generally not a best practice to remove foundation records. However, if you prefer not to see inactive users in sys_user table, you can set up an archive rule to move them to the archived table. I assume this is being done in non-production environments.

 

Regards,
Siva

View solution in original post

7 REPLIES 7

@attanhes Glad it helped..

Weird
Mega Sage

What you want to always do is that you name your variables in unique ways. Never call your variable "GR" or "current" etc. as you're potentially allowing other BR's to overwrite your current job.
For example if you have a script that loops through incidents with GR variable and then you have an update BR that aborts action for GR, then you're potentially losing each update.

Also, you can prevent BR's from running.
Just add setWorkflow(false) into your script and no BR's are run and deleting is faster. Of course I don't recommend doing it if you have some kind of delete logic in your table that has to be run (deleting related records for example).

var userDelete = new GlideRecord('sys_user');
userDelete.addQuery('active', false);
userDelete.setLimit(10);
userDelete.query();
userDelete.setWorkflow(false);
userDelete.deleteMultiple();

 Try this and see if it feels faster.
Also, before deleting anything, you might want to first log (userDelete.getRowCount()) rowCount to make sure that your script is going to delete the correct records. I always first check the list view on a table with my query and then log the amount of rows to make sure I'm not accidentally deleting wrong records.

AbidSiddiqui
Mega Guru
Mega Guru

Hi @attanhes 

That is correct this process can be slow since there are many related records (like sys_user_role, sys_user_group, approvals, etc.
Also related to Clone Cleanup Scripts , i believe it only run during an actual clone.

This Article may resolve your query - https://www.servicenow.com/community/developer-forum/i-would-like-delete-the-multiple-users-in-the-u...

Thank You