It's because stock status was added when product collection is loading. You can see how it works as the following
- \Magento\Catalog\Model\ProductRepository::getList()
...
$collection->joinAttribute('visibility', 'catalog_product/visibility', 'entity_id', null, 'inner');
$this->collectionProcessor->process($searchCriteria, $collection);
$collection->load();// The stock fillter will be added after this
$collection->addCategoryIds();
...
- \Magento\CatalogInventory\Model\AddStockStatusToCollection::beforeLoad()
public function beforeLoad(Collection $productCollection, $printQuery = false, $logQuery = false)
{
$this->stockHelper->addIsInStockFilterToCollection($productCollection);
return [$printQuery, $logQuery];
}
- \Magento\CatalogInventory\Helper\Stock::addIsInStockFilterToCollection()
public function addIsInStockFilterToCollection($collection)
{
$stockFlag = 'has_stock_status_filter';
if (!$collection->hasFlag($stockFlag)) {
$isShowOutOfStock = $this->scopeConfig->getValue(
\Magento\CatalogInventory\Model\Configuration::XML_PATH_SHOW_OUT_OF_STOCK,
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
);
$resource = $this->getStockStatusResource();
$resource->addStockDataToCollection(
$collection,
!$isShowOutOfStock
);
$collection->setFlag($stockFlag, true);
}
}
As you can see here, Magento has a configuration that allows showing products are out of stock:
If you change Display Out of Stock Products to Yes as my screenshot, you will have all products that include products are out of stock