{"record":{"id":"226f1d4a1f53b59f","repo":"flowable/flowable-engine","slug":"case-instance-is-still-running-cannot-delete","errorCode":null,"errorMessage":"Case instance is still running, cannot delete ","messagePattern":"Case instance is still running, cannot delete ","errorType":"exception","errorClass":"FlowableException","httpStatus":null,"severity":"error","filePath":"modules/flowable-cmmn-engine/src/main/java/org/flowable/cmmn/engine/impl/cmd/DeleteHistoricCaseInstanceCmd.java","lineNumber":59,"sourceCode":"    @Override\n    public Object execute(CommandContext commandContext) {\n        if (caseInstanceId == null) {\n            throw new FlowableIllegalArgumentException(\"caseInstanceId is null\");\n        }\n        // Check if case instance is still running\n        CmmnEngineConfiguration cmmnEngineConfiguration = CommandContextUtil.getCmmnEngineConfiguration(commandContext);\n        HistoricCaseInstanceEntity instance = cmmnEngineConfiguration.getHistoricCaseInstanceEntityManager().findById(caseInstanceId);\n\n        if (instance == null) {\n            throw new FlowableObjectNotFoundException(\"No historic case instance found with id: \" + caseInstanceId, HistoricCaseInstance.class);\n        }\n        if (instance.isDeleted()) {\n            return null;\n        }\n\n\n        if (instance.getEndTime() == null) {\n            throw new FlowableException(\"Case instance is still running, cannot delete \" + instance);\n        }\n\n        cmmnEngineConfiguration.getCmmnHistoryManager().recordHistoricCaseInstanceDeleted(caseInstanceId, instance.getTenantId());\n\n        return null;\n    }\n\n}\n","sourceCodeStart":41,"sourceCodeEnd":68,"githubUrl":"https://github.com/flowable/flowable-engine/blob/d6d39ce1c69ff244f2d9dc6af756a9b95e865586/modules/flowable-cmmn-engine/src/main/java/org/flowable/cmmn/engine/impl/cmd/DeleteHistoricCaseInstanceCmd.java#L41-L68","documentation":"DeleteHistoricCaseInstanceCmd refuses to delete a historic case instance whose endTime is null, because that means the case is still running (only the historic mirror exists). Deleting running-case history would corrupt state, so a FlowableException is thrown with the instance appended to the message.","triggerScenarios":"Calling deleteHistoricCaseInstance(id) for a case instance that has not yet completed/terminated (no end time recorded).","commonSituations":"Cleanup scripts deleting history without filtering on finished cases; deleting history for an active case by mistake; assuming 'historic record exists' implies 'case is finished'.","solutions":["Terminate/complete the case first, or delete the running case instance via the runtime API, then delete history","Only target historic cases with a non-null endTime, e.g. query historicCaseInstanceQuery().finished()","Skip still-running instances in batch cleanup scripts instead of failing the whole run"],"exampleFix":"// before\nhistoryService.deleteHistoricCaseInstance(caseId);\n// after\nHistoricCaseInstance hci = historyService.createHistoricCaseInstanceQuery().caseInstanceId(caseId).singleResult();\nif (hci != null && hci.getEndTime() != null) {\n    historyService.deleteHistoricCaseInstance(caseId);\n}","handlingStrategy":"validation","validationCode":"HistoricCaseInstance hci = historyService.createHistoricCaseInstanceQuery().caseInstanceId(id).singleResult();\nif (hci != null && hci.getEndTime() == null) { throw new IllegalStateException(\"case still running: \" + id); }","typeGuard":"boolean isFinished(HistoricCaseInstance hci) { return hci != null && hci.getEndTime() != null; }","tryCatchPattern":"try { historyService.deleteHistoricCaseInstance(id); }\ncatch (FlowableException e) { if (e.getMessage() != null && e.getMessage().startsWith(\"Case instance is still running\")) { /* defer until case ends */ } else throw e; }","preventionTips":["Query with .finished() before deleting historic cases","Terminate running cases via runtime API before deleting their history","Skip unfinished instances in batch history cleanup"],"tags":["cmmn","history","invalid-state-transition","running-case"],"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-14T05:17:10.506Z"}