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

tony_barratt
ServiceNow Employee
ServiceNow Employee

Hi Shikha,



One step I would take would be to run this first, and check the rowcount   printed out was the same as the number found in the list view.



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();


gs.log("row count = " + gr.getRowCount());



Best Regards



Tony


Hi Tony,



I did check the row count before deleting the users with below script:


var encodedQuery="emailISEMPTY^user_nameISEMPTY^ldap_serverISEMPTY";  


var gr = new GlideRecord("sys_user");  


gr.addEncodedQuery(encodedQuery);


gr.query();


gs.print('total rows=='+gr.getRowCount());



and i got the same count 2083 users that i got while applying filters to user table.



I tried this on DEV instance and found everything okay. but i have 1 query, that some people on this post have suggested e to make those users deactive and not delete them. i need to understand, why i should not delete them.



my snow is integrated with AD through LDAP and in AD we have near about 5000 users and in SNOW we are having 9000 users through which it is obvious that 4000 users are those which we don't need.



So, i ran a filter and checked that there are 2083 users whose Emial, User-Id, Ldap-server is null as these three attributes are mapped with attributes of AD. So, i thought to delete these users as they are of no use.



Please suggest what shall i do?


Hey Shikha,



You can go and delete the users but any idea how they got in snow in the first place?


Hi Mujtaba,



Actually when our SNOW was not integrated with AD, the team added the users manually.



When it got integrated with AD users came from AD to SNOW on the basis of mapping of some attributes of AD and SNOW(e.g. Email, User ID, Location etc). so the team didn't delete the users that were added manually which caused the issue of Double profiles of users in SNOW.



That is why i wanted to remove these users.