protected void deleteTask(String taskId) {
    TaskEntity task = Context.getCommandContext().getTaskManager().findTaskById(taskId);

    if (task != null) {
      if (task.getExecutionId() != null) {
        throw new ProcessEngineException(
            "The task cannot be deleted because is part of a running process");
      }

      String reason =
          (deleteReason == null || deleteReason.length() == 0)
              ? TaskEntity.DELETE_REASON_DELETED
              : deleteReason;
      task.delete(reason, cascade);
    } else if (cascade) {
      Context.getCommandContext()
          .getHistoricTaskInstanceManager()
          .deleteHistoricTaskInstanceById(taskId);
    }
  }