{"record":{"id":"193406de84f64bea","repo":"apache/dolphinscheduler","slug":"delete-environment-related-task-exists","errorCode":"DELETE_ENVIRONMENT_RELATED_TASK_EXISTS","errorMessage":"Status.DELETE_ENVIRONMENT_RELATED_TASK_EXISTS","messagePattern":"Status\\.DELETE_ENVIRONMENT_RELATED_TASK_EXISTS","errorType":"error_code","errorClass":"ServiceException","httpStatus":null,"severity":"error","filePath":"dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/EnvironmentServiceImpl.java","lineNumber":290,"sourceCode":"    /**\n     * delete environment\n     *\n     * @param loginUser login user\n     * @param code environment code\n     */\n    @Transactional\n    @Override\n    public void deleteEnvironmentByCode(User loginUser, Long code) {\n        if (!canOperatorPermissions(loginUser, null, AuthorizationType.ENVIRONMENT, ENVIRONMENT_DELETE)) {\n            throw new ServiceException(Status.USER_NO_OPERATION_PERM);\n        }\n\n        long relatedTaskNumber = taskDefinitionDao.countByEnvironmentCode(code);\n\n        if (relatedTaskNumber > 0) {\n            log.warn(\"Delete environment failed because {} tasks is using it, environmentCode:{}.\",\n                    relatedTaskNumber, code);\n            throw new ServiceException(Status.DELETE_ENVIRONMENT_RELATED_TASK_EXISTS);\n        }\n\n        int delete = environmentMapper.deleteByCode(code);\n        if (delete <= 0) {\n            log.error(\"Environment delete error, environmentCode:{}.\", code);\n            throw new ServiceException(Status.DELETE_ENVIRONMENT_ERROR);\n        }\n        relationMapper.delete(new QueryWrapper<EnvironmentWorkerGroupRelation>()\n                .lambda()\n                .eq(EnvironmentWorkerGroupRelation::getEnvironmentCode, code));\n        log.info(\"Environment and relations delete complete, environmentCode:{}.\", code);\n    }\n\n    /**\n     * update environment\n     *\n     * @param loginUser login user\n     * @param code environment code","sourceCodeStart":272,"sourceCodeEnd":308,"githubUrl":"https://github.com/apache/dolphinscheduler/blob/02eac45a1b6676e639fcbfb4be2243de5771b05d/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/EnvironmentServiceImpl.java#L272-L308","documentation":"Thrown by EnvironmentServiceImpl.deleteEnvironmentByCode when task definitions still reference the environment (taskDefinitionDao.countByEnvironmentCode > 0). Environments in use by tasks cannot be deleted to avoid breaking workflow execution. The event is logged with the related task count and code before throwing.","triggerScenarios":"DELETE /dolphinscheduler/environments/{code} where one or more rows in t_ds_task_definition have environment_code equal to the target code; deleting an environment that tasks were configured to run with.","commonSituations":"Cleanup scripts removing 'unused' environments that are in fact referenced by dormant task definitions; environments referenced by tasks imported from another cluster; users unaware that task definitions keep environment_code even when a workflow is not running.","solutions":["Find task definitions using the environment (query task definitions with that environment code) and reassign or clear their environment setting, then retry the delete.","Delete or update the referencing task definitions first (via UI or task-definition update API), then delete the environment.","If the environment should stay, skip the delete and keep it — this error is protective."],"exampleFix":"// before\nenvironmentService.deleteEnvironmentByCode(loginUser, code); // throws if in use\n// after\nlong inUse = taskDefinitionDao.countByEnvironmentCode(code);\nif (inUse > 0) {\n    taskDefinitions.forEach(td -> td.setEnvironmentCode(null)); // reassign/clear first\n}\nenvironmentService.deleteEnvironmentByCode(loginUser, code);","handlingStrategy":"validation","validationCode":"public boolean environmentInUse(long code) {\n    return taskDefinitionDao.countByEnvironmentCode(code) > 0;\n}\n// Only call delete when environmentInUse(code) == false","typeGuard":null,"tryCatchPattern":"try {\n    environmentService.deleteEnvironmentByCode(loginUser, code);\n} catch (ServiceException e) {\n    if (String.valueOf(e.getMessage()).contains(\"DELETE_ENVIRONMENT_RELATED_TASK_EXISTS\")) {\n        // reassign or clear environment on the referencing task definitions first\n    }\n}","preventionTips":["Before deleting, query task definitions referencing the environment code and reassign them","Prefer updating tasks to a replacement environment rather than deleting the old one","Run cleanup scripts in report-only mode first to list in-use environments"],"tags":["dolphinscheduler","referential-integrity","delete","environment"],"backgroundTag":"resource-in-use","analyzedSha":"02eac45a1b6676e639fcbfb4be2243de5771b05d","analyzedAt":"2026-09-06T17:43:00.555Z","contentChangedAt":"2026-09-06T17:43:00.555Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}