{"record":{"id":"27337a6608b39ce6","repo":"flowable/flowable-engine","slug":"cannot-delete-job-when-the-job-is-being-executed","errorCode":null,"errorMessage":"Cannot delete job when the job is being executed. Try again later.","messagePattern":"Cannot delete job when the job is being executed\\. Try again later\\.","errorType":"exception","errorClass":"ActivitiException","httpStatus":null,"severity":"warning","filePath":"modules/flowable5-engine/src/main/java/org/activiti/engine/impl/cmd/DeleteJobCmd.java","lineNumber":68,"sourceCode":"\r\n    protected JobEntity getJobToDelete(CommandContext commandContext) {\r\n        if (jobId == null) {\r\n            throw new ActivitiIllegalArgumentException(\"jobId is null\");\r\n        }\r\n        if (LOGGER.isDebugEnabled()) {\r\n            LOGGER.debug(\"Deleting job {}\", jobId);\r\n        }\r\n\r\n        JobEntity job = commandContext.getJobEntityManager().findJobById(jobId);\r\n        if (job == null) {\r\n            throw new ActivitiObjectNotFoundException(\"No job found with id '\" + jobId + \"'\", Job.class);\r\n        }\r\n\r\n        // We need to check if the job was locked, ie acquired by the job acquisition thread\r\n        // This happens if the job was already acquired, but not yet executed.\r\n        // In that case, we can't allow to delete the job.\r\n        if (job.getLockOwner() != null) {\r\n            throw new ActivitiException(\"Cannot delete job when the job is being executed. Try again later.\");\r\n        }\r\n        return job;\r\n    }\r\n\r\n}\r\n","sourceCodeStart":50,"sourceCodeEnd":74,"githubUrl":"https://github.com/flowable/flowable-engine/blob/d6d39ce1c69ff244f2d9dc6af756a9b95e865586/modules/flowable5-engine/src/main/java/org/activiti/engine/impl/cmd/DeleteJobCmd.java#L50-L74","documentation":"DeleteJobCmd refuses to delete a job whose lockOwner is set, meaning the async job executor's acquisition thread has already acquired the job and it is executing (or about to execute). Deleting it mid-execution could corrupt processing, so the command throws ActivitiException telling the caller to retry later. This is a deliberate concurrency guard, not a bug.","triggerScenarios":"Calling ManagementService.deleteJob(jobId) (or similar delete APIs routed through DeleteJobCmd) while the async job executor has acquired the job — i.e. JOB_.LOCK_OWNER_ and LOCK_EXP_TIME_ are set and unexpired.","commonSituations":"Admin canceling a stuck-looking job while the job executor is actively running it; clustered setups where another node's acquisition thread grabbed the job; deleting a job immediately after triggering an async continuation.","solutions":["Wait briefly and retry the deletion after the job finishes (poll until lockOwner is null)","Stop/suspend the job executor (ProcessEngineConfiguration asyncExecutorActive=false) before deleting","Clear the lock manually by updating ACT_RU_JOB to null out LOCK_OWNER_/LOCK_EXP_TIME_ during maintenance downtime, then delete","Catch ActivitiException and implement retry with backoff"],"exampleFix":"// before\nmanagementService.deleteJob(jobId);\n// after\nJob job = managementService.createJobQuery().jobId(jobId).singleResult();\nif (job != null && job.getLockOwner() == null) {\n    managementService.deleteJob(jobId);\n} else {\n    // retry later or suspend the job executor first\n}","handlingStrategy":"retry","validationCode":"Job job = managementService.createJobQuery().jobId(jobId).singleResult();\nboolean locked = job != null && job.getLockOwner() != null;","typeGuard":"boolean deletable(Job job) { return job != null && job.getLockOwner() == null; }","tryCatchPattern":"try {\n    managementService.deleteJob(jobId);\n} catch (ActivitiException e) {\n    if (e.getMessage().contains(\"being executed\")) { /* schedule retry with backoff */ }\n}","preventionTips":["Check lockOwner before deleting","Suspend/stop the async job executor for maintenance deletions","Use retry with backoff rather than immediate re-delete"],"tags":["job","concurrency","job-executor","locking"],"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-18T11:17:12.947Z"}