{"record":{"id":"8772bc3dbbfe4ec2","repo":"flowable/flowable-engine","slug":"process-instance-is-still-running-cannot-delete-h","errorCode":null,"errorMessage":"Process instance is still running, cannot delete historic process instance: ${processInstanceId}","messagePattern":"Process instance is still running, cannot delete historic process instance: (.+?)","errorType":"exception","errorClass":"ActivitiException","httpStatus":null,"severity":"error","filePath":"modules/flowable5-engine/src/main/java/org/activiti/engine/impl/cmd/DeleteHistoricProcessInstanceCmd.java","lineNumber":51,"sourceCode":"    public DeleteHistoricProcessInstanceCmd(String processInstanceId) {\n        this.processInstanceId = processInstanceId;\n    }\n\n    @Override\n    public Object execute(CommandContext commandContext) {\n        if (processInstanceId == null) {\n            throw new ActivitiIllegalArgumentException(\"processInstanceId is null\");\n        }\n        // Check if process instance is still running\n        HistoricProcessInstance instance = commandContext\n                .getHistoricProcessInstanceEntityManager()\n                .findHistoricProcessInstance(processInstanceId);\n\n        if (instance == null) {\n            throw new ActivitiObjectNotFoundException(\"No historic process instance found with id: \" + processInstanceId, HistoricProcessInstance.class);\n        }\n        if (instance.getEndTime() == null) {\n            throw new ActivitiException(\"Process instance is still running, cannot delete historic process instance: \" + processInstanceId);\n        }\n\n        commandContext\n                .getHistoricProcessInstanceEntityManager()\n                .deleteHistoricProcessInstanceById(processInstanceId);\n\n        return null;\n    }\n\n}\n","sourceCodeStart":33,"sourceCodeEnd":62,"githubUrl":"https://github.com/flowable/flowable-engine/blob/d6d39ce1c69ff244f2d9dc6af756a9b95e865586/modules/flowable5-engine/src/main/java/org/activiti/engine/impl/cmd/DeleteHistoricProcessInstanceCmd.java#L33-L62","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Delete/cancel the runtime process instance first (runtimeService.deleteProcessInstance(id, reason)) so it gets an endTime, then delete history.","Only select finished instances: HistoricProcessInstanceQuery.finished() before deleting.","If the instance is stuck, migrate or remove it via the runtime APIs/admin tooling, then retry history deletion."],"exampleFix":"// before\nhistoryService.deleteHistoricProcessInstance(processInstanceId);\n// after\nif (runtimeService.createProcessInstanceQuery().processInstanceId(processInstanceId).count() > 0) {\n    runtimeService.deleteProcessInstance(processInstanceId, \"purge before history delete\");\n}\nhistoryService.deleteHistoricProcessInstance(processInstanceId);","handlingStrategy":"validation","validationCode":"HistoricProcessInstance hpi = historyService.createHistoricProcessInstanceQuery()\n        .processInstanceId(processInstanceId).finished().singleResult();\nif (hpi == null) {\n    throw new IllegalStateException(\"Instance not finished or not found; resolve runtime instance first\");\n}","typeGuard":null,"tryCatchPattern":"try {\n    historyService.deleteHistoricProcessInstance(id);\n} catch (ActivitiException e) {\n    if (e.getMessage().contains(\"still running\")) {\n        runtimeService.deleteProcessInstance(id, \"purge\");\n        historyService.deleteHistoricProcessInstance(id);\n    } else { throw e; }\n}","preventionTips":["Only purge history for instances with endTime != null","Investigate stuck instances instead of trying to delete their history directly","Order cleanup: runtime deletion first, history deletion second"],"tags":["history","process-instance","still-running","invalid-state"],"backgroundTag":"invalid-state-transition","analyzedSha":"d6d39ce1c69ff244f2d9dc6af756a9b95e865586","analyzedAt":"2026-09-11T06:41:19.413Z","contentChangedAt":"2026-09-11T06:41:19.413Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}