K2 Process Instance Removal Tool

  • 1 August 2019
  • 0 replies

I’ve a question from our customer about delete process instance from server in bulk. If we use process instance management in K2 Management site, we can delete only 20 items and we need to select the process that we want to delete. If we want to delete many processes, it’ll take a lot of time.


                                     Process Instance Management in Management Site


How To Do?

If we want to do bulk delete, how do we do?

The easiest way is delete record in table Server.ProcInst but this way record in related tables will not deleted. So we didn’t recommend this way.

Recommended way was delete record in related tables then delete record in Server.ProcInst but it’s hard to delete in all related tables. So we’ll do with API.


Solution in API

Firstly, how we can get all process instance? For this question, we have “Process Instance” SmartObject that K2 provide.


                                                         Process Instance SmartObject


We’ll use this SmartObjct to list all process instance that we want to delete. In our case, we’ll filter from process instance’s start date and process instance’s status. Coding that we’ll use will be below.

SourceCode.Data.SmartObjectsClient.SOConnectionStringBuilder builder = new SourceCode.Data.SmartObjectsClient.SOConnectionStringBuilder();

builder.Server = “localhost”;

builder.Port = 5555;

DataTable dtProcInst = new DataTable();

using (SourceCode.Data.SmartObjectsClient.SOConnection con = new SourceCode.Data.SmartObjectsClient.SOConnection(builder.ConnectionString))


string stmt = @”SELECT * FROM Process_Instance WHERE StartDate BETWEEN @startDate AND @endDate AND Status = ‘“ + status + “‘“;

SourceCode.Data.SmartObjectsClient.SOCommand command = new SourceCode.Data.SmartObjectsClient.SOCommand(stmt, con);




SourceCode.Data.SmartObjectsClient.SODataAdapter adapter = new SourceCode.Data.SmartObjectsClient.SODataAdapter(command);

command.Parameters.Add(“@startDate”, DbType.DateTime);

command.Parameters.Add(“@endDate”, DbType.DateTime);

command.Parameters[“@startDate”].Value = startDate;

command.Parameters[“@endDate”].Value = endDate;









We’ll have list of process instance that we want to delete and loop to delete process instance using class WorkflowManagementServer.

SourceCode.Workflow.Management.WorkflowManagementServer wfmServer = new SourceCode.Workflow.Management.WorkflowManagementServer();





for (int i = 0; i < dtProcInst.Rows.Count; i++)


DataRow dr = dtProcInst.Rows[i];

AddLog(“ProcInstID : “ + dr[“ProcessInstanceID”].ToString() + “ StartDate: “ + dr[“StartDate”].ToString() + “ Process Name: “ + dr[“ProcessName”].ToString() + “ Folio: “ + dr[“Folio”].ToString());

wfmServer.DeleteProcessInstances(int.Parse(dr[“ProcessInstanceID”].ToString()), true);


AddLog(“@@@@@Record Count: “ + dtProcInst.Rows.Count.ToString());




catch (Exception ex)


AddLog(“Error: “ + ex.Message);




You can create application using code above. If you want ready to use application, you can download at https://drive.google.com/open?id=1Y9SSDqzZ1iS3UmQpNrSJqVX6g-l8XmJO


                                                           Process Instance Removal Tool


Application will let you select date range to search with process instance’s start date and process instance’s status. Then we can click “Process” button to delete process instance. Application will write process instance delete log in application’s directory.


If you have a chance to use application, please give feedback to me :)

0 replies

Be the first to reply!