{"record":{"id":"b0bc0889656fae3f","repo":"flowable/flowable-engine","slug":"the-task-cannot-be-deleted-because-is-part-of-a-ru","errorCode":null,"errorMessage":"The task cannot be deleted because is part of a running process","messagePattern":"The task cannot be deleted because is part of a running process","errorType":"exception","errorClass":"ActivitiException","httpStatus":null,"severity":"error","filePath":"modules/flowable5-engine/src/main/java/org/activiti/engine/impl/persistence/entity/TaskEntityManager.java","lineNumber":202,"sourceCode":"\n    public long findTaskCountByNativeQuery(Map<String, Object> parameterMap) {\n        return (Long) getDbSqlSession().selectOne(\"selectTaskCountByNativeQuery\", parameterMap);\n    }\n\n    @SuppressWarnings(\"unchecked\")\n    public List<Task> findTasksByParentTaskId(String parentTaskId) {\n        return getDbSqlSession().selectList(\"selectTasksByParentTaskId\", parentTaskId);\n    }\n\n    public void deleteTask(String taskId, String deleteReason, boolean cascade) {\n        TaskEntity task = Context\n                .getCommandContext()\n                .getTaskEntityManager()\n                .findTaskById(taskId);\n\n        if (task != null) {\n            if (task.getExecutionId() != null) {\n                throw new ActivitiException(\"The task cannot be deleted because is part of a running process\");\n            }\n\n            String reason = (deleteReason == null || deleteReason.length() == 0) ? TaskEntity.DELETE_REASON_DELETED : deleteReason;\n            deleteTask(task, reason, cascade);\n        } else if (cascade) {\n            Context\n                    .getCommandContext()\n                    .getHistoricTaskInstanceEntityManager()\n                    .deleteHistoricTaskInstanceById(taskId);\n        }\n    }\n\n    public void updateTaskTenantIdForDeployment(String deploymentId, String newTenantId) {\n        HashMap<String, Object> params = new HashMap<>();\n        params.put(\"deploymentId\", deploymentId);\n        params.put(\"tenantId\", newTenantId);\n        getDbSqlSession().update(\"updateTaskTenantIdForDeployment\", params);\n    }","sourceCodeStart":184,"sourceCodeEnd":220,"githubUrl":"https://github.com/flowable/flowable-engine/blob/d6d39ce1c69ff244f2d9dc6af756a9b95e865586/modules/flowable5-engine/src/main/java/org/activiti/engine/impl/persistence/entity/TaskEntityManager.java#L184-L220","documentation":"TaskEntityManager.deleteTask(taskId, ...) refuses to delete a standalone task whose executionId is not null, because such a task belongs to a running process instance and its lifecycle is owned by the engine. Deleting it directly would corrupt the process state, so the engine throws this ActivitiException. Only standalone tasks (created via TaskService.newTask) may be deleted through this path; process tasks disappear when their execution completes or the instance is deleted.","triggerScenarios":"Calling taskService.deleteTask(taskId) (or deleteTasks) where the task was created by a process (user task in a running process instance), detected via task.getExecutionId() != null.","commonSituations":"Admin cleanup scripts trying to remove stale user tasks from active process instances; trying to 'cancel' a task instead of completing it or deleting the process instance; tests deleting seeded process tasks.","solutions":["Delete the owning process instance instead: runtimeService.deleteProcessInstance(executionId, reason)","Complete the task (or resolve and complete) rather than deleting it","Use runtimeService.createChangeActivityStateBuilder() or move activity to a terminate end event to legally remove the activity","Only call deleteTask on standalone tasks (executionId == null)"],"exampleFix":"// before\ntaskService.deleteTask(taskId); // throws for process tasks\n// after\nTask task = taskService.createTaskQuery().taskId(taskId).singleResult();\nif (task.getExecutionId() != null) {\n    runtimeService.deleteProcessInstance(task.getProcessInstanceId(), \"cancelled by admin\");\n} else {\n    taskService.deleteTask(taskId);\n}","handlingStrategy":"validation","validationCode":"Task t = taskService.createTaskQuery().taskId(taskId).singleResult();\nboolean deletable = t != null && t.getExecutionId() == null;","typeGuard":null,"tryCatchPattern":"try {\n    taskService.deleteTask(taskId);\n} catch (ActivitiException e) {\n    if (e.getMessage().contains(\"part of a running process\")) {\n        runtimeService.deleteProcessInstance(processInstanceId, reason);\n    }\n}","preventionTips":["Check task.getExecutionId() before deleting","Delete the process instance to remove process-owned tasks","Expose delete only for standalone tasks in admin tooling"],"tags":["task","process-instance","lifecycle","state-machine"],"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"}