An exception occurred when I tried to delete the process instance

**

public MetaWorkData taskRefuse(MetaWorkData metaWorkData, Map<String, Object> variables) {
    taskComplete(metaWorkData, variables);

    MetaWorkFlow metaWorkFlow = workFlowService.queryById(metaWorkData.getFlow().getId());
    MetaWorkFlowAssert.EMPTY_META_WORK_FLOW.isNotBlank(metaWorkFlow);

    List<MetaWorkFlow> allWorkFlows = new ArrayList<>();
    collectParentAndChildWorkflows(metaWorkFlow, allWorkFlows);
    MetaWorkFlow topLevelParent = findTopLevelParent(metaWorkFlow);
    String processInstanceId = topLevelParent.getThirdBusinessKey();
    HistoricProcessInstance historicProcessInstance = processEngine.getHistoryService()
            .createHistoricProcessInstanceQuery()
            .processInstanceId(processInstanceId)
            .singleResult();
    MetaWorkFlowAssert.FLOWABLE_PROCESS_INSTANCE_EMPTY.isNotBlank(historicProcessInstance);
    processEngine.getRuntimeService().deleteProcessInstance(processInstanceId, "Fail the audit");

    allWorkFlows.forEach(flow -> {
        doAfterProcessInstanceChange(flow, FlowStatusTypeEnum.REFUSED);
    });

    return metaWorkData;
}

**
when do deleteProcessInstance::org.flowable.common.engine.api.FlowableObjectNotFoundException: No process instance found for id ‘xxxxx’

Hey @MoringMartin,

On which line does it fail? Is it in

processEngine.getRuntimeService().deleteProcessInstance(processInstanceId, "Fail the audit");

or in

allWorkFlows.forEach(flow -> {
    doAfterProcessInstanceChange(flow, FlowStatusTypeEnum.REFUSED);});

Cheers,
Filip

yes,just in this line. processEngine.getRuntimeService().deleteProcessInstance(processInstanceId, “Fail the audit”);

However, from the database, the corresponding process instance is real. I do not understand why this situation occurs

OK, now I see it. You are using the runtime service to perform the deletion. The error means that the process instance is already completed and thus the process cannot be deleted.

You’ll need to use the history service to delete completed processes (they’ll be completely removed from the DB then).

You can use the ProcessInstanceQuery to query for runtime processes or add unfinished() to your historic process instance query.

Cheers,
Filip

Ok, I should use ProcessInstanceQuery。I will try it now