reindex after post api magento 2 1

reindex after post api magento 2

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $indexerCollectionFactory = $objectManager->get("\Magento\Indexer\Model\Indexer\CollectionFactory");
    $indexerFactory = $objectManager->get("\Magento\Indexer\Model\IndexerFactory");

    $indexerCollection = $indexerCollectionFactory->create();
    $allIds = $indexerCollection->getAllIds();
    foreach ($allIds as $id) {
        $indexer = $indexerFactory->create()->load($id);
        $indexer->reindexAll();
    }

Here is what the above code is Doing:
1. Get the object manager
2. Get the indexer collection factory
3. Get the indexer factory
4. Get the indexer collection
5. Get all the indexer ids
6. Loop through all the indexer ids
7. Load the indexer
8. Reindex all

You can also reindex a single indexer by using the following code:

Similar Posts