below is the the implemeation
public void execute(DelegateExecution delegateExecution) throws Exception {
System.out.println("--->"+ "deleting...");
try {
deleteUnusedDeployments(delegateExecution);
} catch (RuntimeException e) {
throw new BpmnError("cleanupException", "Message: " + e.getMessage() + ", Stacktrace: " + e.getStackTrace());
}
}
private void deleteUnusedDeployments(DelegateExecution delegateExecution) {
System.out.println("--->"+repositoryService.createDeploymentQuery().list());
List<Deployment> deployments = repositoryService.createDeploymentQuery().list();
System.out.println("--->"+ deployments.get(0));
for (Deployment deployment : deployments) {
boolean deploymentCanBeDeleted = true;
List<ProcessDefinition> processDefinitions = repositoryService
.createProcessDefinitionQuery()
.deploymentId(deployment.getId()).list();
System.out.println("--->"+ repositoryService
.createProcessDefinitionQuery()
.deploymentId(deployment.getId()).list());
for (ProcessDefinition processDefinition : processDefinitions) {
ProcessDefinition latestProcessDefiniton = repositoryService
.createProcessDefinitionQuery()
.processDefinitionKey(processDefinition.getKey())
.latestVersion().singleResult();
boolean isLatest = latestProcessDefiniton.getId().equals(processDefinition.getId());
boolean hasRunningInstances = runtimeService
.createProcessInstanceQuery()
.processDefinitionId(processDefinition.getId()).count() > 0;
boolean hasHistoricInstances = historyService
.createHistoricProcessInstanceQuery()
.processDefinitionId(processDefinition.getId()).count() > 0;
if (isLatest || hasRunningInstances || hasHistoricInstances) {
deploymentCanBeDeleted = false;
break;
}
}
if (deploymentCanBeDeleted) {
//repositoryService.deleteDeployment(deployment.getId());
System.out.println("deleted tem successfully");
}
}
}
}
Camunda:
Below Error getting:
—>deleting…
2021-10-11 21:48:20.714 INFO 29836 — [aTaskExecutor-1] org.camunda.bpm.engine.bpmn.behavior : ENGINE-02001 Execution with id ‘say-hello’ throws an error event with errorCode ‘cleanupException’, but no catching boundary event was defined. Execution is ended (none end event semantics).