- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-03-2015 09:32 AM
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();
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-03-2015 10:29 AM
If you do go with deactivating, rather than deleting, I'd do it through the UI:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-03-2015 06:54 PM
There is tons of bad practice in this script. You should always wrap your script into functions so it does not run the risk of hijacking another query. This is especially important if you're doing something like a delete. Additionally, I would advise you put print statements in on the first dry run without the actual delete committing that way you can see which records you're actually going to be deleting first hand. Below is what I would recommend you do:
deleteUser();
function deleteUsers(){
var target = new GlideRecord('sys_user');
target.addNullQuery('user_name');
target.addNullQuery('email');
target.addNullQuery('ldap_server');
target.query();
while(target.next()){
gs.print("We are deleting the following user: " + target.user_name);
target.deleteRecord();
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-03-2015 08:01 PM
Just wanted to add that if we are querying for user_name="", it doesn't make sense to say "gs.print("We are deleting the following user: " + target.user_name);" because it will always be blank.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-03-2015 08:16 PM
This is true! Use target.sys_id instead!
Regards!