flowable/flowable-engine · error · ActivitiException

Process instance is still running, cannot delete historic pr

Error message

Process instance is still running, cannot delete historic process instance: ${processInstanceId}

What it means

History deletion is only allowed for finished process instances. The command checks instance.getEndTime(); if the process instance is still running (no end time in ACT_HI_PROCINST), it refuses with this ActivitiException. You must terminate the runtime instance first; history rows for live processes cannot be deleted independently.

Source

Thrown at modules/flowable5-engine/src/main/java/org/activiti/engine/impl/cmd/DeleteHistoricProcessInstanceCmd.java:51

    public DeleteHistoricProcessInstanceCmd(String processInstanceId) {
        this.processInstanceId = processInstanceId;
    }

    @Override
    public Object execute(CommandContext commandContext) {
        if (processInstanceId == null) {
            throw new ActivitiIllegalArgumentException("processInstanceId is null");
        }
        // Check if process instance is still running
        HistoricProcessInstance instance = commandContext
                .getHistoricProcessInstanceEntityManager()
                .findHistoricProcessInstance(processInstanceId);

        if (instance == null) {
            throw new ActivitiObjectNotFoundException("No historic process instance found with id: " + processInstanceId, HistoricProcessInstance.class);
        }
        if (instance.getEndTime() == null) {
            throw new ActivitiException("Process instance is still running, cannot delete historic process instance: " + processInstanceId);
        }

        commandContext
                .getHistoricProcessInstanceEntityManager()
                .deleteHistoricProcessInstanceById(processInstanceId);

        return null;
    }

}

View on GitHub (pinned to d6d39ce1c6)

Solutions

  1. Delete/cancel the runtime process instance first (runtimeService.deleteProcessInstance(id, reason)) so it gets an endTime, then delete history.
  2. Only select finished instances: HistoricProcessInstanceQuery.finished() before deleting.
  3. If the instance is stuck, migrate or remove it via the runtime APIs/admin tooling, then retry history deletion.

Example fix

// before
historyService.deleteHistoricProcessInstance(processInstanceId);
// after
if (runtimeService.createProcessInstanceQuery().processInstanceId(processInstanceId).count() > 0) {
    runtimeService.deleteProcessInstance(processInstanceId, "purge before history delete");
}
historyService.deleteHistoricProcessInstance(processInstanceId);
Defensive patterns

Strategy: validation

Validate before calling

HistoricProcessInstance hpi = historyService.createHistoricProcessInstanceQuery()
        .processInstanceId(processInstanceId).finished().singleResult();
if (hpi == null) {
    throw new IllegalStateException("Instance not finished or not found; resolve runtime instance first");
}

Try / catch

try {
    historyService.deleteHistoricProcessInstance(id);
} catch (ActivitiException e) {
    if (e.getMessage().contains("still running")) {
        runtimeService.deleteProcessInstance(id, "purge");
        historyService.deleteHistoricProcessInstance(id);
    } else { throw e; }
}

Prevention

When it happens

Trigger: Calling deleteHistoricProcessInstance(id) while the process instance is still active in the runtime tables (no endTime recorded) — e.g. a long-running, waiting, or stuck instance.

Common situations: Cleanup jobs that iterate all history rows without filtering on finished(); trying to purge data for instances suspended or stuck at a wait state; deleting history of a failed migration left in runtime tables.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


AI-assisted analysis of flowable/flowable-engine@d6d39ce1c6 (2026-09-11). Data as JSON: /api/errors/8772bc3dbbfe4ec2. Report an issue: GitHub.