Purge Old Completed Process Instances

Introduction

This guide explains how to automate the deletion of old completed process instances in Joget using a Bean Shell script. By scheduling this script, you can maintain optimal server performance by removing outdated data.

How does it work?

To keep your server performance optimal, it's essential to purge old completed process instances regularly. The following Bean Shell script automatically deletes process instances older than 12 months, helping you manage server resources effectively.

import org.joget.workflow.model.service.WorkflowManager;
import org.joget.workflow.model.WorkflowProcess;
import org.joget.apps.app.service.AppService;
import org.joget.apps.app.service.AppUtil;
import org.joget.commons.util.LogUtil;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
 
AppService appService = (AppService) AppUtil.getApplicationContext().getBean("appService");
WorkflowManager workflowManager = (WorkflowManager) AppUtil.getApplicationContext().getBean("workflowManager");
 
String appId = null; //to filter by specific appId
String processId = null; //to filter by specific processId
String processName = null;
String version = null;
String recordId = null;
String requester = null;
String sort = "Started";
boolean desc = false; //find oldest first
int start = 0;
int rows = 15;
 
int months = 12; //retain last X months data AKA remove data older than X months
Date cutOffDate = Date.from(LocalDate.now().minusMonths(months).atStartOfDay(ZoneId.systemDefault()).toInstant());
 
int removedCount = 0;
Collection removedPids = new ArrayList();
 
LogUtil.info("Purge Old Completed Process Data ", "Searching for completed process instances older than " + cutOffDate.toString());
 
boolean newerThanCutOffFound = false;
 
while(!newerThanCutOffFound){
    //get 15 oldest records
    Collection processList = workflowManager.getCompletedProcessList(appId, processId, processName, version, recordId, requester, sort, desc, start, rows);
    for (WorkflowProcess workflowProcess : processList) {
        String id = workflowProcess.getInstanceId();
         
        Date startedTime = workflowProcess.getStartedTime();
         
        if( cutOffDate.compareTo(startedTime) == 1){
            //startedTime is older than cutOffDate, remove process data
             
            LogUtil.info("Purge Old Process Data", "Found [" + id + "] with start time [" + startedTime + "]");
            removedCount++;
            removedPids.add(id);
            appService.getAppDefinitionForWorkflowProcess(id);
            workflowManager.removeProcessInstance(id);
        }else{
            newerThanCutOffFound = true;
            break;
        }
         
    }
    //uncomment below to run only once for testing
    //newerThanCutOffFound = true;
}
 
LogUtil.info("Purge Old Completed Process Data ", "Completed with [" + removedCount + "] removed: " + removedPids.toString());

You can adjust the script to fit specific conditions or retain data for a different duration.

Integrate the script into a scheduler job or process tool to automate the purging process. If needed, ensure the script is configured to filter by specific app or process IDs.

The script retrieves process instances and compares their start times to a cutoff date. Instances older than the cutoff date are deleted, and a log is generated for the removed instances.

You may modify lines String appId = null//to filter by specific appId to String requester = null; if you would like to filter according to specific conditions.

You may also modify line int months = 12//retain last X months data AKA remove data older than X months and change how many months of the latest process instances you would like to retain.

Sample server log

After running the script, the server log should display entries similar to the following:

INFO  22 Mar 2022 14:23:54 Purge Old Process Data  - Searching for completed process instances older than Wed Dec 22 00:00:00 MYT 2021
INFO  22 Mar 2022 14:23:54 Purge Old Process Data - Found [68_ap_auditInitial_approver_process] with start time [Fri Aug 31 02:15:14 MYT 2018]
INFO  22 Mar 2022 14:23:55 Purge Old Process Data - Found [69_ap_auditInitial_approver_process] with start time [Fri Aug 31 02:17:24 MYT 2018]
INFO  22 Mar 2022 14:23:55 Purge Old Process Data - Found [70_ap_auditInitial_approver_process] with start time [Fri Aug 31 02:22:06 MYT 2018]
INFO  22 Mar 2022 14:23:55 Purge Old Process Data - Found [74_ap_auditInitial_approver_process] with start time [Fri Aug 31 02:25:35 MYT 2018]
INFO  22 Mar 2022 14:23:55 Purge Old Process Data - Found [75_ap_actionPlanCreationProcess] with start time [Fri Aug 31 02:26:06 MYT 2018]
INFO  22 Mar 2022 14:23:55 Purge Old Process Data - Found [76_ap_actionPlanCreationProcess] with start time [Fri Aug 31 02:26:07 MYT 2018]
INFO  22 Mar 2022 14:23:55 Purge Old Process Data - Found [77_ap_auditInitial_approver_process] with start time [Fri Aug 31 03:12:59 MYT 2018]
INFO  22 Mar 2022 14:23:55 Purge Old Process Data - Found [78_ap_actionPlanCreationProcess] with start time [Fri Aug 31 03:13:52 MYT 2018]
INFO  22 Mar 2022 14:23:55 Purge Old Process Data - Found [79_ap_actionPlanCreationProcess] with start time [Fri Aug 31 03:13:52 MYT 2018]
INFO  22 Mar 2022 14:23:55 Purge Old Process Data - Found [82_ap_auditInitial_approver_process] with start time [Fri Aug 31 03:27:29 MYT 2018]
INFO  22 Mar 2022 14:23:55 Purge Old Process Data - Found [83_ap_actionPlanCreationProcess] with start time [Fri Aug 31 03:28:04 MYT 2018]
INFO  22 Mar 2022 14:23:55 Purge Old Process Data - Found [84_ap_actionPlanCreationProcess] with start time [Fri Aug 31 03:28:05 MYT 2018]
INFO  22 Mar 2022 14:23:55 Purge Old Process Data - Found [86_ap_auditInitial_approver_process] with start time [Fri Aug 31 04:12:40 MYT 2018]
INFO  22 Mar 2022 14:23:55 Purge Old Process Data - Found [87_ap_actionPlanCreationProcess] with start time [Fri Aug 31 04:13:11 MYT 2018]
INFO  22 Mar 2022 14:23:55 Purge Old Process Data - Found [88_ap_actionPlanCreationProcess] with start time [Fri Aug 31 04:13:11 MYT 2018]
INFO  22 Mar 2022 14:23:56 Purge Old Process Data  - Completed with [15] removed: [68_ap_auditInitial_approver_process, 69_ap_auditInitial_approver_process, 70_ap_auditInitial_approver_process, 74_ap_auditInitial_approver_process, 75_ap_actionPlanCreationProcess, 76_ap_actionPlanCreationProcess, 77_ap_auditInitial_approver_process, 78_ap_actionPlanCreationProcess, 79_ap_actionPlanCreationProcess, 82_ap_auditInitial_approver_process, 83_ap_actionPlanCreationProcess, 84_ap_actionPlanCreationProcess, 86_ap_auditInitial_approver_process, 87_ap_actionPlanCreationProcess, 88_ap_actionPlanCreationProcess]
Created by Julieth Last modified by Aadrian on Dec 13, 2024