Auto.arima with xreg in R, restriction on forecast periods
Using xreg suggests that you have external (exogenous) variables. In this, a regression model is fitted to the external variables with ARIMA errors.

When forecasting you need to provide future values of these external variables. In practice, these are often forecasts or could be known. For example, if you're trying to predict Sales and you use Advertising spend as an external variable, you may know the advertising spend for the upcoming year.

auto.arima then produces forecasts for the length of xreg, therefore disregarding h.

Based on your comments below, I've provided an example script demonstrating this based on the Sales example above.

library(forecast)

# Generate sample data
sales &lt;- sample(100:170, 4*10, replace = TRUE)
advertising &lt;- sample(50:70, 4*10, replace = TRUE)

# Create time series objects.
sales_ts &lt;- ts(sales, frequency = 4, end = c(2017, 4))


fit &lt;- auto.arima(sales_ts, xreg = advertising)

# If we pass external_regressor into the forecast, h will be disregarded and we will
# get a forecast for length(external_regressor)

wrong_forecast = forecast(fit, h = 4, xreg = advertising)

length(wrong_forecast) # Will be 40

# To forecast four quarters in advance, we must provide forecasted external regressor data
# for the upcoming four quarters, so that length(new_regressor) == 4.
# In reality, this data is either forecasted from another forecast, or is known. We'll randomly generate it.

upcoming_advertising &lt;- sample(50:70, 4, replace = TRUE)

correct_forecast &lt;- forecast(fit, xreg = upcoming_advertising)

length(correct_forecast$mean) # Will be 4


The key things to note are:

If we forecast with the same regressors as we did when generating the forecast, h will be disregarded and a forecast will be generated for the length of xreg in your case, 10 years.

As such, we must provide new data for xreg for the length of time we wish to forecast - in your case, 4 quarters.