bpm/src/main/java/org/jboss/pnc/bpm/BpmManager.java
public void cleanup() { //TODO remove tasks immediately after the completion, see: BuildTaskEndpoint.buildTaskCompleted
        log.debug("Bpm manager tasks cleanup started");

        if (session == null) {
            log.error("Kie session not available.");
        }

        Map<Integer, BpmTask> clonedTaskMap = new HashMap<>(this.tasks);

        Set<Integer> toBeRemoved = clonedTaskMap.values().stream()
                .filter(bpmTask -> {
                    if (bpmTask == null) {
                        log.warn("Listing invalid entry for removal from the tasks list.");
                        return true;
                    }
                    log.debug("Attempting to fetch process instance for bpmTask: {}.", bpmTask.getTaskId());
                    Long processInstanceId = bpmTask.getProcessInstanceId();
                    ProcessInstance processInstance = session.getProcessInstance(processInstanceId);
                    log.debug("fetched: {}", processInstance);
                    if (processInstance == null) // instance has been terminated from outside
                        return true;
                    int state = processInstance.getState();
                    return state == STATE_COMPLETED || state == STATE_ABORTED;
                })
                .map(BpmTask::getTaskId)
                .collect(Collectors.toSet());
        toBeRemoved.forEach(id -> {
            BpmTask removed = tasks.remove(id);
            if (removed != null) {
                log.debug("Removed bpmTask.id: {}.", removed.getTaskId());
            } else {
                log.warn("Unable to remove bpmTask.id: {}.", id);
            }
        });

        log.debug("Bpm manager tasks cleanup finished");
    }
public void cleanup() { //TODO remove tasks immediately after the completion, see: BuildTaskEndpoint.buildTaskCompleted
        log.debug("Bpm manager tasks cleanup started");

        if (session == null) {
            log.error("Kie session not available.");
        }

        Map<Integer, BpmTask> clonedTaskMap = new HashMap<>(this.tasks);

        Set<Integer> toBeRemoved = clonedTaskMap.values().stream()
                .filter(bpmTask -> {
                    if (bpmTask == null) {
                        log.warn("Listing invalid entry for removal from the tasks list.");
                        return true;
                    }
                    log.debug("Attempting to fetch process instance for bpmTask: {}.", bpmTask.getTaskId());
                    Long processInstanceId = bpmTask.getProcessInstanceId();
                    ProcessInstance processInstance = session.getProcessInstance(processInstanceId);
                    log.debug("fetched: {}", processInstance);
                    if (processInstance == null) // instance has been terminated from outside
                        return true;
                    int state = processInstance.getState();
                    return state == STATE_COMPLETED || state == STATE_ABORTED;
                })
                .map(BpmTask::getTaskId)
                .collect(Collectors.toSet());
        toBeRemoved.forEach(id -> {
            BpmTask removed = tasks.remove(id);
            if (removed != null) {
                log.debug("Removed bpmTask.id: {}.", removed.getTaskId());
            } else {
                log.warn("Unable to remove bpmTask.id: {}.", id);
            }
        });

        log.debug("Bpm manager tasks cleanup finished");
    }
Regularly cleans finished BPM tasks asynchronously
Immediate cleanup is not usable because of NCL-2300