function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Jason Dodds CodeScienceJason Dodds CodeScience 

Delete paused Flow interview from Apex

Using Process Builder, I have a series of Processes(Flows) that execute in 2 days, 4 days, etc (email campaign). if certain criteria (call it Criteria A) are met.  The problem with this approach (other than not having the ability to create multiple schedules in a single process) is that a process/flow is in a "paused interview" state until the delay time (2 days) and the filter criteria (Criteria A) are met.  While this technically solves my business scenario (don't execute the flow if the user has done X), it results in all of the resulting flows to remain in the "paused interview" state indefinitely.  There is a limit of 30K "paused interviews" at one time, so my question is "how do I progammatically clean-up interviews that I do not need to run any longer?"  I don't see any Apex method for removing these via a scheduled batch or other means.

We are using the Flows to create records that drive transactional emails, otherwise this process is perfect for a time-based Workflow.  Given the lack of functionality to create a record or call a headless flow from time-based workflow, I'm at a loss finding another route for doing this declaratively.

Thanks,

Jason
ShashankShashank (Salesforce Developers) 
I woulld recommend you to try the new Process Builder and see if it does the trick for you: https://help.salesforce.com/HTViewHelpDoc?id=process_overview.htm
Jason Dodds CodeScienceJason Dodds CodeScience
I am using the Process Builder.
Derek Davis 7Derek Davis 7
Hello Jason! Did you ever figure this out? I am running into the exact same issue.
 
Jason Dodds CodeScienceJason Dodds CodeScience

Hi Derek. To answer your question, no, I never figured this out.  I'm not sure, but I think in the last release you are allowed to have multiple schedules for an action and it looks like if a process doesn't meet the triggering criteria, the schedule action is dropped from the queue according to this doc (https://help.salesforce.com/HTViewHelpDoc?id=process_limits_scheduled.htm&language=en_US):

 

  • For processes that are set to run when a record is created or edited, scheduled actions remain in the queue only as long as the criteria for those actions are still valid. If a record no longer matches the criteria, Salesforce removes the scheduled actions for that record from the queue.

That should reduce the overall number of paused interviews, but I didn't do any more research on programmatically removing those via Apex.  Good luck, and let me know if there is anything else I can help out with.

Derek Davis 7Derek Davis 7
Thanks, Jason! This is very helpful.

Currently - my process is set to only run when created. So it looks like I need to figure out a way to tweak so that it can run when Created or Edited.
  • For processes that are set to run when a record is created, Salesforce never reevaluates the record with that process. Scheduled actions remain in the queue, even if the record no longer meets the associated criteria when the scheduled actions are executed.
Avinash VatsyaAvinash Vatsya

Pass the flow Ids into the method one by one. you can create a batch, or limit the loop by 10. (number of apex callout per transaction)

public void deleteFlow(Id Ids)
{
 //HTTP Request
    HttpRequest request = new HttpRequest();
    request.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionID());
    request.setHeader('Content-Type', 'application/json');
    request.setEndpoint(URL.getSalesforceBaseUrl().toExternalForm()+'/services/data/v45.0/tooling/sobjects/Flow/'+ids);
    request.setMethod('DELETE');
    //HTTP Response
    Http poll = new Http();
    HttpResponse res = poll.send(request);
    System.Debug('**** flow Response: ' + res.getBody());
}

 

Query to get flow :

SELECT DefinitionId,id,Status,VersionNumber,MasterLabel from Flow where VersionNumber >1 AND Status ='Obsolete'

This will get you all inactive flow, modify accordingly.