flowable/flowable-engine · error · FlowableException

Process instance is still running, cannot delete

Error message

Process instance is still running, cannot delete 

What it means

DeleteHistoricProcessInstanceCmd only deletes historic records; if the historic instance has no endTime the process is still running and the engine throws FlowableException. Deleting a running instance from history is not allowed; cancel it via the runtime API instead.

Source

Thrown at modules/flowable-engine/src/main/java/org/flowable/engine/impl/cmd/DeleteHistoricProcessInstanceCmd.java:56

        this.processInstanceId = processInstanceId;
    }

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

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

        if (Flowable5Util.isFlowable5ProcessDefinitionId(commandContext, instance.getProcessDefinitionId())) {
            Flowable5CompatibilityHandler compatibilityHandler = Flowable5Util.getFlowable5CompatibilityHandler();
            compatibilityHandler.deleteHistoricProcessInstance(processInstanceId);
            return null;
        }

        CommandContextUtil.getHistoryManager(commandContext).recordProcessInstanceDeleted(processInstanceId, instance.getProcessDefinitionId(), instance.getTenantId());

        return null;
    }

}

View on GitHub (pinned to d6d39ce1c6)

Solutions

  1. Use runtimeService.deleteProcessInstance(id, reason) to terminate a running instance first, then delete its history
  2. Check HistoricProcessInstance.getEndTime() != null before calling delete
  3. Filter queries with .finished() when bulk-deleting historic instances

Example fix

// before
historyService.deleteHistoricProcessInstance(id); // instance still running
// after
if (runtimeService.createProcessInstanceQuery().processInstanceId(id).count() > 0) {
    runtimeService.deleteProcessInstance(id, "cleanup");
}
historyService.deleteHistoricProcessInstance(id);
Defensive patterns

Strategy: validation

Validate before calling

HistoricProcessInstance hpi = historyService.createHistoricProcessInstanceQuery()
    .processInstanceId(id).singleResult();
if (hpi != null && hpi.getEndTime() == null) {
    runtimeService.deleteProcessInstance(id, "cancel before history delete");
}

Type guard

boolean isFinished = id -> {
    HistoricProcessInstance h = historyService.createHistoricProcessInstanceQuery()
        .processInstanceId(id).singleResult();
    return h != null && h.getEndTime() != null;
};

Try / catch

try {
    historyService.deleteHistoricProcessInstance(id);
} catch (FlowableException e) {
    if (e.getMessage().contains("still running")) {
        runtimeService.deleteProcessInstance(id, "terminated by cleanup");
        historyService.deleteHistoricProcessInstance(id);
    }
}

Prevention

When it happens

Trigger: historyService.deleteHistoricProcessInstance(id) where the process instance has started but not finished (endTime is null), typically an id pointing at an active instance.

Common situations: Calling history deletion while the process is still executing, cleanup scripts that iterate ids without checking endTime, or confusion between deleteProcessInstance (runtime) and deleteHistoricProcessInstance (history).

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/39347cbf7631d8dd. Report an issue: GitHub.