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

Delete data from table based on condition

Ruchi19
Mega Contributor

Hi,
Is there anyway to delete the data from table based on the filter criteria? I have around 8K records to be deleted out of 15K..Manually deleting each one of them would be so laborious.
Any suggestion?

3 REPLIES 3

MB26
ServiceNow Employee
ServiceNow Employee

You could write a background script and delete each record.
http://wiki.service-now.com/index.php?title=GlideRecord#Delete_Methods
Here is an example from that page.



var rec = new GlideRecord('incident');
rec.addQuery('active',false);
rec.query();
while (rec.next()) {
gs.print('Inactive incident ' + rec.number + ' deleted');
rec.deleteRecord();
}


If you are not familiar with how to query, you will want to read up on
http://wiki.service-now.com/index.php?title=GlideRecord#Query_Methods

In "System Definition --> Scripts - -Background" you will be able to run this script to delete a lot of records.

As an FYI ---> Be careful doing this. Make sure your query is correct, as deleting 8000 records can be a big oops if your delete query is not correct.


Ruchi19
Mega Contributor

Thanks a lot ...that helps!


Ivan Martez
ServiceNow Employee
ServiceNow Employee

Here is another good reference for performing queries.
http://www.servicenowguru.com/scripting/gliderecord-query-cheat-sheet/