Going back in workflow


I have a need to go back to a previous stage in the workflow upon choose to go back and the subsequent task should be rest. if there are any completed tasks that should also get rest.
eg in the below WF the process is already at last stage and task A is completed while B is not if I get an amendment request and the workflow is pointed back to step 2 then I get Task A and task B twice after finishing step 2. how to rest the flow after redirecting it to step 2 from amendment task

        .moveActivityIdTo(task.getTaskDefinitionKey(), historicTaskInstance.getTaskDefinitionKey())

You can use this function.