{"record":{"id":"2f2062d09722423e","repo":"apache/dolphinscheduler","slug":"delete-workflow-definition-executing-fail","errorCode":"DELETE_WORKFLOW_DEFINITION_EXECUTING_FAIL","errorMessage":"DELETE_WORKFLOW_DEFINITION_EXECUTING_FAIL: can not be deleted because there are {0} executing instances","messagePattern":"DELETE_WORKFLOW_DEFINITION_EXECUTING_FAIL: can not be deleted because there are (.+?) executing instances","errorType":"error_code","errorClass":"ServiceException","httpStatus":null,"severity":"error","filePath":"dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/WorkflowDefinitionServiceImpl.java","lineNumber":857,"sourceCode":"    /**\n     * workflow definition want to delete whether used in other task, should throw exception when have be used.\n     * <p>\n     * This function avoid delete workflow definition already dependencies by other tasks by accident.\n     *\n     * @param workflowDefinition WorkflowDefinition you change task definition and task relation\n     */\n    private void workflowDefinitionUsedInOtherTaskValid(User loginUser, WorkflowDefinition workflowDefinition) {\n        // check workflow definition is already online\n        if (workflowDefinition.getReleaseState() == ReleaseState.ONLINE) {\n            throw new ServiceException(Status.WORKFLOW_DEFINE_STATE_ONLINE, workflowDefinition.getName());\n        }\n\n        // check workflow instances is already running\n        List<WorkflowInstanceSummaryDto> workflowInstances =\n                workflowInstanceService.queryByWorkflowDefinitionCodeAndStatus(\n                        workflowDefinition.getCode(), WorkflowExecutionStatus.NOT_TERMINAL_STATES);\n        if (CollectionUtils.isNotEmpty(workflowInstances)) {\n            throw new ServiceException(Status.DELETE_WORKFLOW_DEFINITION_EXECUTING_FAIL, workflowInstances.size());\n        }\n\n        // check workflow used by other task, including sub workflow and dependent task type\n        Optional<String> taskDepMsg = workflowLineageService.taskDependentMsg(loginUser,\n                workflowDefinition.getProjectCode(), workflowDefinition.getCode(), 0);\n\n        if (taskDepMsg.isPresent()) {\n            String errorMeg = \"workflow definition cannot be deleted because it has dependent, \" + taskDepMsg.get();\n            log.error(errorMeg);\n            throw new ServiceException(errorMeg);\n        }\n    }\n\n    public void deleteWorkflowDefinitionByCode(User loginUser, long code) {\n        WorkflowDefinition workflowDefinition = workflowDefinitionDao.queryByCode(code)\n                .orElseThrow(() -> new ServiceException(WORKFLOW_DEFINITION_NOT_EXIST, String.valueOf(code)));\n\n        Project project = projectDao.queryByCode(workflowDefinition.getProjectCode());","sourceCodeStart":839,"sourceCodeEnd":875,"githubUrl":"https://github.com/apache/dolphinscheduler/blob/02eac45a1b6676e639fcbfb4be2243de5771b05d/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/WorkflowDefinitionServiceImpl.java#L839-L875","documentation":"Thrown when the workflow definition still has non-terminal workflow instances (running, waiting, etc.). The service queries workflowInstanceService.queryByWorkflowDefinitionCodeAndStatus with NOT_TERMINAL_STATES and refuses deletion while executions are in flight, reporting the instance count in the message.","triggerScenarios":"Deleting a workflow whose scheduler kicked off instances still executing, or manually-started runs that have not finished (or are stuck in a non-terminal state).","commonSituations":"Deleting during a long-running ETL workflow; stuck/zombie instances left in RUNNING state after a master crash; deleting a workflow right after triggering it in a test.","solutions":["Wait for the running instances to finish, then delete.","Force-stop or kill the executing instances via the workflow-instance API, then retry deletion.","For zombie instances, correct their state to a terminal value (admin action) and delete again."],"exampleFix":"// before\nworkflowDefinitionService.deleteWorkflowDefinitionByCode(loginUser, code); // instances running\n// after\nList<WorkflowInstanceSummaryDto> running = workflowInstanceService\n    .queryByWorkflowDefinitionCodeAndStatus(code, WorkflowExecutionStatus.NOT_TERMINAL_STATES);\nrunning.forEach(i -> workflowInstanceService.forceStop(i.getId(), loginUser));\nworkflowDefinitionService.deleteWorkflowDefinitionByCode(loginUser, code);","handlingStrategy":"validation","validationCode":"List<WorkflowInstanceSummaryDto> running = workflowInstanceService\n    .queryByWorkflowDefinitionCodeAndStatus(code, WorkflowExecutionStatus.NOT_TERMINAL_STATES);\nif (!running.isEmpty()) {\n    running.forEach(i -> workflowInstanceService.forceStop(i.getId(), user));\n}","typeGuard":null,"tryCatchPattern":"try { service.deleteWorkflowDefinitionByCode(user, code); }\ncatch (ServiceException e) { if (e.getCode() == Status.DELETE_WORKFLOW_DEFINITION_EXECUTING_FAIL) { /* stop instances then retry */ } }","preventionTips":["Check for non-terminal instances before deletion","Clean up zombie instances periodically","Avoid deleting workflows right after triggering runs"],"tags":["workflow-definition","running-instances","state-guard"],"backgroundTag":"invalid-state-transition","analyzedSha":"02eac45a1b6676e639fcbfb4be2243de5771b05d","analyzedAt":"2026-09-06T17:43:00.555Z","contentChangedAt":"2026-09-06T17:43:00.555Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}