This question already has an answer here:

I need to run command php bin/magento indexer:reindex in Controller.

marked as duplicate by Jaimin Sutariya, Teja Bhagavan Kollepara, Jai, Murtuza Zabuawala, Shoaib Munir May 17 at 8:51

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
  • @RutveeSojitra the problem is that I need to call this command in code – Tomalo May 17 at 6:50
  • means you don't want to reindexing? – Rutvee Sojitra May 17 at 6:51
  • @RutveeSojitra I want to reindex but I want to do this by calling the command. I do not want to wait for the page to refresh whem reindexing – Tomalo May 17 at 6:57
  • @SavanPatel no working I don't want to wait for refresh page – Tomalo May 17 at 7:16

Run Reindex command in Controller Put below code into your controller and you can run when you need it

public function reindexAll() {
    $indexerFactory = $this->_objectManager->get('Magento\Indexer\Model\IndexerFactory');
    $indexerIds = array(
    foreach ($indexerIds as $indexerId) {
        echo " create index: ".$indexerId."\n";
        $indexer = $indexerFactory->create();

Not the answer you're looking for? Browse other questions tagged or ask your own question.