Best way to delete multiple record in servicenow

MK-p
Tera Contributor

Hi,

 

I want to delete around 20k record, could you please help me the best way to do so. I dont want to make my system slow during deletion.

1 ACCEPTED SOLUTION

Runjay Patel
Giga Sage

Hi @MK-p ,

My recommendation would be instead of deleting those record you can archive it. Benefit of archive is you can refer any point of time in future and if these records are not relevant to your client then you can use below code to delete.

 

Create a fix script and use below code.

 

var grObj = new GlideRecord('your_table_name');
grObj .addEncodedQuery('your_encoded_query'); // Specify the condition for the records to delete
 grObj .deleteMultiple();

//For Example
//if i want to delete all incident having no short description then my code would be like this.
var incGr = new GlideRecord('incident');
    incGr.addNullQuery('short_description');
    incGr.deleteMultiple();

 

 

 

Please Mark Correct if this solves your query and also mark Helpful if you find my response worthy based on the impact.

Regards,

Runjay Patel 

View solution in original post

2 REPLIES 2

Shubham_Jain
Mega Sage

@MK-p There are many ways to do this - 

 

1. Use the Data Archiving Plugin (If Available): If your use case allows, consider archiving the records instead of outright deletion. Archiving reduces the load on the system while retaining records for future reference.

  • Navigate to System Archiving and set up an archive rule for the table where you want to remove records.
  • This will move the records to an archive table and can significantly reduce the impact on system performance.

2. Scheduled Script Execution: Deleting records in smaller batches through Scheduled Jobs is one of the safest ways to avoid system slowdown. This spreads the load across multiple executions.

Here’s how you can schedule a script to delete records in batches:

  • Go to System Definition > Scheduled Jobs > Scheduled Script Execution.
  • Create a new script with something like this:

 

 

// Define the number of records you want to delete in each batch
var batchSize = 1000;

// Define the condition for the records you want to delete
var gr = new GlideRecord('your_table_name');
gr.addEncodedQuery('your_encoded_query'); // Specify the condition for the records to delete
gr.query();

var counter = 0;
while (gr.next() && counter < batchSize) {
    gr.deleteRecord();
    counter++;
}

// Log how many records were deleted
gs.info('Deleted ' + counter + ' records');

 

 

 

 

    • Set up this script to run periodically (e.g., every 5 or 10 minutes) until all 20k records are deleted.
    • Adjust the batchSize to a number that suits your instance’s performance capacity. Start small (e.g., 500-1000) and monitor performance, then adjust the batch size accordingly.

3. Use the Delete UI Action with Filters (for Ad-hoc Deletion): If you want to perform the deletion manually, but still want to do it in smaller chunks, you can use the Delete UI action on a filtered list of records.

  • Go to the list view of the table where the records are stored.
  • Apply a filter to identify the records you want to delete.
  • Use the Delete UI action to delete a manageable number of records at a time. Do not select all 20,000 records at once; instead, select smaller batches (e.g., 500 or 1000 at a time).

4. Background Scripts for Controlled Deletion: You can also run a Background Script to delete the records in batches. Similar to Scheduled Jobs, but this is more manual and for immediate execution.

Go to System Definition > Scripts - Background, and run the following script:

 

 

 

var gr = new GlideRecord('your_table_name');
gr.addEncodedQuery('your_encoded_query'); // Define the condition for the records you want to delete
gr.query();

var count = 0;
while (gr.next()) {
    gr.deleteRecord();
    count++;
    if (count >= 1000) {  // Delete 1000 records at a time
        gs.info("Deleted 1000 records, pausing for a while...");
        break;  // You can run the script again for the next batch
    }
}

gs.info("Deleted " + count + " records.");

 

 

 

  • This method deletes records in controlled batches (adjust 1000 as needed) to prevent system overload.
  • Run the script multiple times until all records are deleted.

 

 

✔️ If this solves your issue, please mark it as Correct.


✔️ If you found it helpful, please mark it as Helpful.



Shubham Jain


Runjay Patel
Giga Sage

Hi @MK-p ,

My recommendation would be instead of deleting those record you can archive it. Benefit of archive is you can refer any point of time in future and if these records are not relevant to your client then you can use below code to delete.

 

Create a fix script and use below code.

 

var grObj = new GlideRecord('your_table_name');
grObj .addEncodedQuery('your_encoded_query'); // Specify the condition for the records to delete
 grObj .deleteMultiple();

//For Example
//if i want to delete all incident having no short description then my code would be like this.
var incGr = new GlideRecord('incident');
    incGr.addNullQuery('short_description');
    incGr.deleteMultiple();

 

 

 

Please Mark Correct if this solves your query and also mark Helpful if you find my response worthy based on the impact.

Regards,

Runjay Patel