What’s the best way to cancel bulk RITM tickets and their flow executions?

MandyMaseyM
Tera Contributor

Hi everyone, our catalog form is using Flow Designer instead of Workflow when creating RITM/SCTASK tickets.

 

Now, we have 5,000+ invalid RITM tickets that we need to cancel, along with their flow executions. I just want to ask what the best way is to cancel them.

 

I was thinking of using a background script, but I think it's too risky, especially since we have to do it in the PROD instance. Can you suggest a better approach? Thank you. 🙂

3 REPLIES 3

vaishali231
Tera Guru

Hey @MandyMaseyM 

For cancelling a large number of RITMs (5,000+) along with their associated Flow Designer executions, you’re right to be cautious—especially in a PROD environment. A direct background script can be risky without proper control and auditability.

 

 Approach

1. Avoid Direct Background Scripts in PROD

While background scripts can work, they:

  • Do not provide audit tracking
  • Have no rollback mechanism
  • Can impact performance if large volumes are processed at once

 

2. Use a Fix Script with Controlled Logic

A Fix Script is the preferred approach because it:

  • Can be reviewed and deployed via update set
  • Runs in a controlled, one-time execution
  • Provides better traceability

 

3. Cancel Flow Executions Using API (Important)

Flow Designer executions should not be cancelled by directly updating tables like sys_flow_context.

Instead, use:

  • sn_fd.FlowAPI.cancel()

This ensures flows are terminated safely without leaving orphaned executions.

 

4. Process Records in Batches

To avoid performance issues:

  • Process records in smaller chunks (e.g., 100–200 at a time)
  • Optionally run during off-hours

 

5.  Fix Script 

var ritm = new GlideRecord('sc_req_item');
ritm.addEncodedQuery('YOUR_FILTER_CONDITION'); // Ensure this is well-tested
ritm.setLimit(200); // Batch processing
ritm.query();
while (ritm.next()) {
   // Cancel Flow executions
   var flowContext = new GlideRecord('sys_flow_context');
   flowContext.addQuery('source_record', ritm.sys_id);
   flowContext.query();
   while (flowContext.next()) {
       sn_fd.FlowAPI.cancel(flowContext.sys_id, 'Bulk cancellation');
   }
   // Cancel RITM
   ritm.state = 4; // Cancelled
   ritm.update();
}

 6.  Alternative Option

If you want even more control:

  • Use a Scheduled Script Execution
  • Run periodically with small batches until all records are processed

***************************************************************************************

If this response helps, please mark it as Accept as Solution and Helpful.
Doing so helps others in the community and encourages me to keep contributing.
Regards
Vaishali Singh



hey @MandyMaseyM 

Hope you are doing well.

Did my previous reply answer your question?

If it was helpful, please mark it as correct ✓ and close the thread . This will help other readers find the solution more easily.

Regards,
Vaishali Singh

Hi @vaishali231 thank you so much for responding.

I'm new to ServiceNow and it's my first time hearing about Fix Script. I just want to clarify something

For #3, how do I do this? is this a sys property? How do I use the sn_fd.FlowAPI.cancel()?

 

For #5, is this the actual Fix Script that we can use? We've got the list of RITM numbers, what filter condition should we use? Can't we just manually input the list of RITM numbers?