{"record":{"id":"39347cbf7631d8dd","repo":"flowable/flowable-engine","slug":"process-instance-is-still-running-cannot-delete","errorCode":null,"errorMessage":"Process instance is still running, cannot delete ","messagePattern":"Process instance is still running, cannot delete ","errorType":"exception","errorClass":"FlowableException","httpStatus":null,"severity":"error","filePath":"modules/flowable-engine/src/main/java/org/flowable/engine/impl/cmd/DeleteHistoricProcessInstanceCmd.java","lineNumber":56,"sourceCode":"        this.processInstanceId = processInstanceId;\n    }\n\n    @Override\n    public Object execute(CommandContext commandContext) {\n        if (processInstanceId == null) {\n            throw new FlowableIllegalArgumentException(\"processInstanceId is null\");\n        }\n        // Check if process instance is still running\n        HistoricProcessInstanceEntity instance = CommandContextUtil.getHistoricProcessInstanceEntityManager(commandContext).findById(processInstanceId);\n\n        if (instance == null) {\n            throw new FlowableObjectNotFoundException(\"No historic process instance found with id: \" + processInstanceId, HistoricProcessInstance.class);\n        }\n        if (instance.isDeleted()) {\n            return null;\n        }\n        if (instance.getEndTime() == null) {\n            throw new FlowableException(\"Process instance is still running, cannot delete \" + instance);\n        }\n\n        if (Flowable5Util.isFlowable5ProcessDefinitionId(commandContext, instance.getProcessDefinitionId())) {\n            Flowable5CompatibilityHandler compatibilityHandler = Flowable5Util.getFlowable5CompatibilityHandler();\n            compatibilityHandler.deleteHistoricProcessInstance(processInstanceId);\n            return null;\n        }\n\n        CommandContextUtil.getHistoryManager(commandContext).recordProcessInstanceDeleted(processInstanceId, instance.getProcessDefinitionId(), instance.getTenantId());\n\n        return null;\n    }\n\n}\n","sourceCodeStart":38,"sourceCodeEnd":71,"githubUrl":"https://github.com/flowable/flowable-engine/blob/d6d39ce1c69ff244f2d9dc6af756a9b95e865586/modules/flowable-engine/src/main/java/org/flowable/engine/impl/cmd/DeleteHistoricProcessInstanceCmd.java#L38-L71","documentation":"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.","triggerScenarios":"historyService.deleteHistoricProcessInstance(id) where the process instance has started but not finished (endTime is null), typically an id pointing at an active instance.","commonSituations":"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).","solutions":["Use runtimeService.deleteProcessInstance(id, reason) to terminate a running instance first, then delete its history","Check HistoricProcessInstance.getEndTime() != null before calling delete","Filter queries with .finished() when bulk-deleting historic instances"],"exampleFix":"// before\nhistoryService.deleteHistoricProcessInstance(id); // instance still running\n// after\nif (runtimeService.createProcessInstanceQuery().processInstanceId(id).count() > 0) {\n    runtimeService.deleteProcessInstance(id, \"cleanup\");\n}\nhistoryService.deleteHistoricProcessInstance(id);","handlingStrategy":"validation","validationCode":"HistoricProcessInstance hpi = historyService.createHistoricProcessInstanceQuery()\n    .processInstanceId(id).singleResult();\nif (hpi != null && hpi.getEndTime() == null) {\n    runtimeService.deleteProcessInstance(id, \"cancel before history delete\");\n}","typeGuard":"boolean isFinished = id -> {\n    HistoricProcessInstance h = historyService.createHistoricProcessInstanceQuery()\n        .processInstanceId(id).singleResult();\n    return h != null && h.getEndTime() != null;\n};","tryCatchPattern":"try {\n    historyService.deleteHistoricProcessInstance(id);\n} catch (FlowableException e) {\n    if (e.getMessage().contains(\"still running\")) {\n        runtimeService.deleteProcessInstance(id, \"terminated by cleanup\");\n        historyService.deleteHistoricProcessInstance(id);\n    }\n}","preventionTips":["Check endTime before any historic delete","Use runtimeService APIs for running instances, historyService only for finished ones","In bulk cleanup use .finished() query criteria"],"tags":["flowable","invalid-state","history","process-instance"],"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"}