How do I restart an opened workflow?

Currently I have a workflow that has been opened and it is still in progress.

Due to some business logic, I want to restart the workflow from the beginning using the same workflowId.

If I try to call WorkflowClient.start(…), I get the following error:
Caused by: io.grpc.StatusRuntimeException: ALREADY_EXISTS: Workflow execution is already running

Is there a way to restart a workflow? How do I do that with the Java SDK?

There are multiple ways to restart a workflow. You can either terminate the previous run and start it again.
Another option is to reset the workflow to its first workflow task.

Thanks for the reply, Maxim. Is there a way to restart it programmatically? Also terminate it programmatically?

All operations UI and CLI expose are going through gRPC APIs of the service. All of these APIs are available for user code as well.

For terminations you can use untyped workflow stub:

    WorkflowStub client =
        workflowClient.newUntypedWorkflowStub(workflowId, Optional.of(runId), Optional.empty());
    client.terminate("No reason");

Awesome. You mentioned there’s a reset workflow option through the CLI but I can’t seem to find that in the sdk-java. Could you point me to it? Thanks.

        WorkflowServiceStubs service =
            WorkflowServiceStubs.newInstance(
                WorkflowServiceStubsOptions.newBuilder().setTarget(serviceAddress).build());
        service.blockingStub().resetWorkflowExecution(request);