# An exception occurred when I tried to delete the process instance

**URL:** <https://forum.flowable.org/t/an-exception-occurred-when-i-tried-to-delete-the-process-instance/11632>\
**Category:** Flowable Engine\
**Created:** [July 10, 2024, 3:42am UTC](https://forum.flowable.org/t/an-exception-occurred-when-i-tried-to-delete-the-process-instance/11632 "2024-07-10T03:42:06Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![MoringMartin](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.flowable.org/moringmartin/32/3521_2.png) [@MoringMartin](https://forum.flowable.org/u/MoringMartin)\
**Post date:** [July 10, 2024, 3:42am UTC](https://forum.flowable.org/t/an-exception-occurred-when-i-tried-to-delete-the-process-instance/11632/1 "2024-07-10T03:42:06Z")

</div>

\*\*

```
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’

---

<div class="post-metadata">

**Author:** ![filiphr](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.flowable.org/filiphr/32/315_2.png) [@filiphr](https://forum.flowable.org/u/filiphr)\
**Post date:** [July 10, 2024, 6:09am UTC](https://forum.flowable.org/t/an-exception-occurred-when-i-tried-to-delete-the-process-instance/11632/3 "2024-07-10T06:09:28Z")

</div>

Hey @MoringMartin,

On which line does it fail? Is it in

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

```

or in

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

```

Cheers,  
Filip

---

<div class="post-metadata">

**Author:** ![MoringMartin](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.flowable.org/moringmartin/32/3521_2.png) [@MoringMartin](https://forum.flowable.org/u/MoringMartin)\
**Post date:** [July 10, 2024, 6:24am UTC](https://forum.flowable.org/t/an-exception-occurred-when-i-tried-to-delete-the-process-instance/11632/4 "2024-07-10T06:24:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![MoringMartin](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.flowable.org/moringmartin/32/3521_2.png) [@MoringMartin](https://forum.flowable.org/u/MoringMartin)\
**Post date:** [July 10, 2024, 6:26am UTC](https://forum.flowable.org/t/an-exception-occurred-when-i-tried-to-delete-the-process-instance/11632/5 "2024-07-10T06:26:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![filiphr](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.flowable.org/filiphr/32/315_2.png) [@filiphr](https://forum.flowable.org/u/filiphr)\
**Post date:** [July 10, 2024, 6:40am UTC](https://forum.flowable.org/t/an-exception-occurred-when-i-tried-to-delete-the-process-instance/11632/6 "2024-07-10T06:40:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![MoringMartin](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.flowable.org/moringmartin/32/3521_2.png) [@MoringMartin](https://forum.flowable.org/u/MoringMartin)\
**Post date:** [July 10, 2024, 6:45am UTC](https://forum.flowable.org/t/an-exception-occurred-when-i-tried-to-delete-the-process-instance/11632/7 "2024-07-10T06:45:46Z")

</div>

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