How can I delete all records from a Table?

Rajanmehta
Mega Guru

Hello,

We are on Helsinki and wanted to delete all the records (> 20k) from a table. How can I do that?

please advise.

Thanks,

Rajan Mehta

1 ACCEPTED SOLUTION

Erik Stolberg
Tera Guru

Delete all records from a table



  • Navigate to System Definition > Tables and Columns.
  • Select the table for which to delete records.Tables and Columns

Note: Some system tables do not allow this method of deleting records, for example, you cannot delete all user records [sys_user]. The list of tables does not include system tables that you cannot delete records from.


  • Click Delete all records.
  • In the confirmation dialog box, enter delete and click OK.

View solution in original post

9 REPLIES 9

Dipu Joy
ServiceNow Employee
ServiceNow Employee

You might get a UI timeout as the Quota is for 5 minutes.


I think the best approach will be to perform this activity as follows:-



Create a script to perform this.


Run this as a Scheduled job so that it will not utilize the worker jobs and will run the background till this is completed.


May be you can split this into 5K records each within the script and run it as a loop.



Make sure that you clone from your PROD to SUB PROD and DRY RUN this on the SUB-PROD before running this on PROD. You also have to take into account about the cascade delete if these records are referenced somewhere. Only the data will be deleted and the stale references will still remain.It will good to test the impact on the SUB PROD.



Hope this helps.


sarah38
Kilo Contributor

I have 180,000 records to delete and the solution given only deletes about 1500 records at a time.

Hello Sarah,

 

you can also use following script to delete all the records.

 

var rec = new GlideRecord ('Table_Name');

rec.addQuery('your_query');// Optional if specific records to delete

rec.query();

gs.info("# of Records:"+ rec.getRowCount());

while(rec.next()){

    rec.deleteRecord();

}

 

Hope this helps.

Vini
Giga Guru

Hi Rajanmehata,

Above script took so long time to complete and ServiceNow instance also stuck. 

We executed this script on 10K records at System Definition > Scripts - Background.

 

Please advise the best way to Cleanup CI.

Note# We have task to delete all CI's created by CredentiallessDiscovery.

 

Regards,

Vinil.

Dipu Joy
ServiceNow Employee
ServiceNow Employee

Hello Vinil,

 

Deleting large data from Background scripts it not a good idea if you know what you are doing.

Scripts running from Background scripts have the Record for Rollback checked by default. Data is deleted using semaphore threads and your instance will be accessible from a different browser window as a different session.

 

For large datasets, create scheduled jobs in chunks OOB hours so it will run in worker threads.

 

This KB will help you with queries on "Mass-Deletion and Excess Data Management Guide"

https://hi.service-now.com/kb_view.do?sysparm_article=KB0717791

Thanks,

Dipu Joy