{"record":{"id":"d1ef595def4d1ff1","repo":"apache/dolphinscheduler","slug":"120034","errorCode":"120034","errorMessage":"this cluster has been used in namespace,so you can't delete it.","messagePattern":"this cluster has been used in namespace,so you can't delete it\\.","errorType":"error_code","errorClass":"ServiceException","httpStatus":null,"severity":"error","filePath":"dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/ClusterServiceImpl.java","lineNumber":199,"sourceCode":"        return dto;\n    }\n\n    /**\n     * delete cluster\n     *\n     * @param loginUser login user\n     * @param code      cluster code\n     */\n    @Override\n    public void deleteClusterByCode(User loginUser, Long code) {\n        if (isNotAdmin(loginUser)) {\n            throw new ServiceException(Status.USER_NO_OPERATION_PERM);\n        }\n\n        long relatedNamespaceNumber = k8sNamespaceDao.countByClusterCode(code);\n\n        if (relatedNamespaceNumber > 0) {\n            throw new ServiceException(Status.DELETE_CLUSTER_RELATED_NAMESPACE_EXISTS);\n        }\n\n        if (clusterDao.deleteByCode(code)) {\n            return;\n        }\n        throw new ServiceException(Status.DELETE_CLUSTER_ERROR);\n    }\n\n    /**\n     * update cluster\n     *\n     * @param loginUser login user\n     * @param code      cluster code\n     * @param name      cluster name\n     * @param config    cluster config\n     * @param desc      cluster desc\n     */\n    @Override","sourceCodeStart":181,"sourceCodeEnd":217,"githubUrl":"https://github.com/apache/dolphinscheduler/blob/02eac45a1b6676e639fcbfb4be2243de5771b05d/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/ClusterServiceImpl.java#L181-L217","documentation":"ClusterServiceImpl.deleteClusterByCode refuses to delete a cluster that is still referenced by one or more K8s namespaces. Before deleting, it counts namespaces linked to the cluster code via k8sNamespaceDao.countByClusterCode(code) and throws ServiceException(Status.DELETE_CLUSTER_RELATED_NAMESPACE_EXISTS, code 120034) when the count is > 0. This is a referential-integrity guard: deleting the cluster would leave orphaned namespace relations.","triggerScenarios":"Calling DELETE on the cluster API endpoint (or ClusterService.deleteClusterByCode) for a cluster whose code appears in the t_ds_k8s_namespace table. Happens even when the namespaces themselves are stale or the cluster is no longer actually running in K8s.","commonSituations":"Admins tearing down an environment: they remove a K8s cluster registration in the UI while namespace records created for that cluster (e.g. during workflow execution with K8s task type) were never cleaned up. Also occurs after partial deletes or when re-registering a cluster under a new code but leaving old namespace rows.","solutions":["Delete all namespaces associated with the cluster first (K8s namespace management page or k8sNamespaceDao.deleteByClusterCode), then retry the cluster delete.","Verify which namespaces reference the cluster by querying the k8s namespace table/list API filtered by cluster code.","If the namespace rows are stale/orphaned (namespace no longer exists in K8s), remove them manually from the database (t_ds_k8s_namespace) and retry.","Only after the related namespace count reaches 0 will deleteClusterByCode succeed."],"exampleFix":"// before: delete cluster directly\nclusterService.deleteClusterByCode(loginUser, clusterCode);\n// after: clean up related namespaces first\nList<K8sNamespace> ns = k8sNamespaceDao.queryByClusterCode(clusterCode);\nif (!ns.isEmpty()) {\n    throw new IllegalStateException(\"Delete related namespaces first: \" + ns.size());\n}\nclusterService.deleteClusterByCode(loginUser, clusterCode);","handlingStrategy":"validation","validationCode":"// Java\nlong nsCount = k8sNamespaceDao.countByClusterCode(clusterCode);\nif (nsCount > 0) {\n    throw new IllegalStateException(\"Cluster \" + clusterCode + \" still has \" + nsCount + \" related namespaces\");\n}","typeGuard":null,"tryCatchPattern":"try {\n    clusterService.deleteClusterByCode(loginUser, code);\n} catch (ServiceException e) {\n    if (e.getCode() == 120034) {\n        // prompt user to delete related namespaces first\n    } else {\n        throw e;\n    }\n}","preventionTips":["Always delete dependent K8s namespaces as part of cluster teardown runbooks","Track namespace-cluster relations in an inventory before deleting","Automate cleanup of stale namespace rows for decommissioned clusters"],"tags":["cluster","k8s","referential-integrity","delete"],"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"}