The CreatorCon Call for Content is officially open! Get started here.

Script used to delete some specific users from snow

geet
Tera Guru

Greetings All,

I have written a script to delete those users whose Email, User ID, Ldap-Server is blank. Please let me know if it is ok.

var encodedQuery="emailISEMPTY^user_nameISEMPTY^ldap_serverISEMPTY";   // semicolon added

var gr = new GlideRecord("sys_user");     //error here " needed to be closed

gr.addEncodedQuery(encodedQuery);

gr.deleteMultiple();

1 ACCEPTED SOLUTION

If you do go with deactivating, rather than deleting, I'd do it through the UI:


Capture.PNG


Capture.PNG


View solution in original post

22 REPLIES 22

If you do go with deactivating, rather than deleting, I'd do it through the UI:


Capture.PNG


Capture.PNG


Cascade Delete Rules are also an option if you are wanting to make sure records will not get orphaned. This may take time to set up, so probably only beneficial if you are aware of the specific tables that would possibly be affected.


Hi rfedoruk,



i don't know how to check this. If you can guide then it will be helpful.


Won't be able to generate the script for you at the moment, but you'd need to cross reference the list of sys_id's you want to delete with any user reference field in any other table.   If you've got any positives you need to either adjust the data before hand.



For example:


- how many tasks have these rogue user accounts had created in their name?   Will your customer be satisfied with "null" showing up as they group task history by data determined by user (cost code, department, company, manager, etc)


- how many tasks have these rogue user accounts had assigned to them?


- what group memberships are these rogue user accounts associated with?


- how many reports are targeted to them?


- how many scheduled jobs send report output to them?


- how many notifications are they hard coded into?


Mike Allen
Mega Sage

But, yes, if you are sure you want to delete every user that has none of these attributes set, that is the way to do it.   Some things that might help you when doing inserts, updates, and deletions are:



setLimit(10); // allows you to perform your action on a sample size before the big delete (in this case, 10


autoSysFields(false);   // Do not update sys_updated_by, sys_updated_on, sys_mod_count, sys_created_by, and sys_created_on


setWorkflow(false);       // Do not run any other business rules




GlideRecord - ServiceNow Wiki