code: private void tryToEraseSimulator() {
      int numTries = 0;
      boolean successfulReset = false;
      while ((!successfulReset) && (numTries < NUMBER_TRIES_RESET_DEVICE)) {
        log.info("Wait " + SLEEP_TIME_BETWEEN_RESET_TRIES + " milliseconds before reattempt.");
        try {
          Thread.sleep(SLEEP_TIME_BETWEEN_RESET_TRIES);
        } catch (InterruptedException e) {
          // ignored
        }
        successfulReset = eraseSimulator();
        numTries += 1;
      }
      if (!successfulReset) {
        int totalWaitTime = SLEEP_TIME_BETWEEN_RESET_TRIES * NUMBER_TRIES_RESET_DEVICE;
        String message = "Erase contents and settings still failed. Total waiting time: "
                + totalWaitTime
                + ". Now try to shutdown device: " + deviceUUID;
        log.warning(message);
  
        Boolean successfulShutdown = shutdownDevice();
        if (successfulShutdown) {
          successfulReset = eraseSimulator();
        }
  
        if (!successfulReset) {
          message = "Unable to erase contents and settings of this device: " + deviceUUID;
          log.warning(message);
          // add more information to exception message
          message += ". Tried " + NUMBER_TRIES_RESET_DEVICE + " times with with a waiting period of " + SLEEP_TIME_BETWEEN_RESET_TRIES
                  + " millisecond between each attempt. ";
          if (!successfulShutdown) {
            message += "Also unable to shut down the device.";
          }
          throw new WebDriverException(message);
        }
      }
    }
docs: Try N times to erase the device (sleeping in between each attempt).
  If that fails, try shutting down the device and then erase again.
  If none of that worked, throw a WebDriverException with a detailed message.