{"record":{"id":"9c4411169ac1e7d1","repo":"apache/dolphinscheduler","slug":"main-table-using-version","errorCode":"MAIN_TABLE_USING_VERSION","errorMessage":"MAIN_TABLE_USING_VERSION: the version is in use by the main table and cannot be deleted","messagePattern":"MAIN_TABLE_USING_VERSION: the version is in use by the main table and cannot be deleted","errorType":"error_code","errorClass":"ServiceException","httpStatus":null,"severity":"error","filePath":"dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/WorkflowDefinitionServiceImpl.java","lineNumber":1738,"sourceCode":"     * @param projectCode project code\n     * @param code        workflow definition code\n     * @param version     version number\n     */\n    @Override\n    @Transactional\n    public void deleteWorkflowDefinitionVersion(User loginUser,\n                                                long projectCode,\n                                                long code,\n                                                int version) {\n        projectService.checkHasProjectWritePermissionThrowException(loginUser, projectCode);\n\n        WorkflowDefinition workflowDefinition = workflowDefinitionDao.queryByCode(code).orElse(null);\n        if (workflowDefinition == null || projectCode != workflowDefinition.getProjectCode()) {\n            throw new ServiceException(Status.WORKFLOW_DEFINITION_NOT_EXIST, code);\n        }\n        if (workflowDefinition.getVersion() == version) {\n            log.warn(\"This version: {} of workflow: {} is the main version cannot delete by version\", code, version);\n            throw new ServiceException(Status.MAIN_TABLE_USING_VERSION);\n        }\n        // check whether there exist running workflow instance under the workflow definition\n        List<WorkflowInstanceSummaryDto> workflowInstances = workflowInstanceService.queryByWorkflowCodeVersionStatus(\n                code,\n                version,\n                WorkflowExecutionStatus.NOT_TERMINAL_STATES);\n        if (CollectionUtils.isNotEmpty(workflowInstances)) {\n            throw new ServiceException(Status.DELETE_WORKFLOW_DEFINITION_EXECUTING_FAIL, workflowInstances.size());\n        }\n\n        int deleteLog = workflowDefinitionLogMapper.deleteByWorkflowDefinitionCodeAndVersion(code, version);\n        int deleteRelationLog = workflowTaskRelationLogMapper.deleteByCode(code, version);\n        if (deleteLog == 0 || deleteRelationLog == 0) {\n            throw new ServiceException(Status.DELETE_WORKFLOW_DEFINE_BY_CODE_ERROR);\n        }\n        log.info(\"Delete version: {} of workflow: {}, projectCode: {}\", version, code, projectCode);\n\n    }","sourceCodeStart":1720,"sourceCodeEnd":1756,"githubUrl":"https://github.com/apache/dolphinscheduler/blob/02eac45a1b6676e639fcbfb4be2243de5771b05d/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/WorkflowDefinitionServiceImpl.java#L1720-L1756","documentation":"Thrown when attempting to delete a workflow definition version that is still the current (main-table) version. Only historical versions recorded in the version/log table can be deleted; the active version must first be replaced by releasing a newer version.","triggerScenarios":"DELETE .../workflow-definition/{projectCode}/{code}/version/{version} where version equals workflowDefinition.getVersion() (the active version in t_ds_workflow_definition).","commonSituations":"Scripts cleaning up old versions that accidentally target the latest version; UI automation that resolves 'version' from the main table instead of from the version history list; deleting the only version a workflow ever had.","solutions":["Delete only non-current versions; list versions first and exclude workflowDefinition.getVersion().","If the current version must go away, release a new version (save/update the workflow) then delete the old one.","To remove the workflow entirely, use deleteWorkflowDefinitionByCode instead of delete-by-version.","Guard scripts: skip a version if it equals the definition's current version."],"exampleFix":"// before\nclient.deleteVersion(projectCode, code, version); // version == current\n// after\nlong current = workflowDefinitionDao.queryByCode(code).orElseThrow().getVersion();\nif (version != current) client.deleteVersion(projectCode, code, version);","handlingStrategy":"validation","validationCode":"WorkflowDefinition wf = workflowDefinitionDao.queryByCode(code).orElse(null);\nboolean canDeleteVersion = wf != null && wf.getVersion() != version;\nif (!canDeleteVersion) skipDelete(code, version);","typeGuard":"boolean isHistoricalVersion(WorkflowDefinition wf, long version) {\n    return wf != null && wf.getVersion() != version;\n}","tryCatchPattern":"try {\n    service.deleteWorkflowDefinitionVersion(user, projectCode, code, version);\n} catch (ServiceException e) {\n    if (e.getCode() == Status.MAIN_TABLE_USING_VERSION.getCode()) {\n        // version is current; release a new version or use delete-by-code instead\n    } else throw e;\n}","preventionTips":["Always fetch the definition's current version and exclude it from cleanup lists.","Use the version-history endpoint as the source of deletable versions.","For full removal use deleteWorkflowDefinitionByCode, not delete-by-version."],"tags":["workflow-definition","versioning","delete-protected","invalid-state-transition"],"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"}