I've tryed to use this example (and other examples on the internet) https://webkul.com/blog/reindexing-programmatically-magento-2/ to execute reindexing programatically.

The case is I want to call reIndexing() from my custom class outside Magento 2 (example), but I almost always get "Uncaught TypeError: Argument 1 passed to CronReindexer::__construct() must be an instance of Magento\\Indexer\\Model\\IndexerFactory, instance of Magento\\Framework\\ObjectManager\\ObjectManager given..."

How to solve this?



require $_SERVER['DOCUMENT_ROOT'].'/app/bootstrap.php';
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
/** @var \Magento\Framework\App\Http $app */
$app = $bootstrap->createApplication('CronReindexer');


class CronReindexer {

    protected $indexerFactory;

    protected $indexerCollectionFactory;

    public function __construct(
        \Magento\Indexer\Model\IndexerFactory $indexerFactory,
        \Magento\Indexer\Model\Indexer\CollectionFactory $indexerCollectionFactory
        $this->indexerFactory = $indexerFactory;
        $this->indexerCollectionFactory = $indexerCollectionFactory;

    public function reIndexing(){
       $indexerCollection = $this->indexerCollectionFactory->create();
       $ids = $indexerCollection->getAllIds();

       foreach ($ids as $indexerId) {
            $indexer = $this->_objectManager->get('Magento\Framework\Indexer\IndexerRegistry')->get($indexerId);



Still doesn't work.


Create the Reindex.php script in the root folder and add below code in this file.

 use Magento\Framework\App\Bootstrap;
 require __DIR__ . '/app/bootstrap.php';

 $params = $_SERVER;
 $bootstrap = Bootstrap::create(BP, $params);
 $obj = $bootstrap->getObjectManager();

 $state = $obj->get('Magento\Framework\App\State');

 $indexer = $obj->get('Magento\Indexer\Model\Indexer\CollectionFactory')->create();
 $ids = $indexer->getAllIds();
 echo '<pre>';

 foreach ($ids as $indexerId) {
     $indexer = $obj->get('Magento\Framework\Indexer\IndexerRegistry')->get($indexerId);
     $indexer->reindexAll($indexerId); // this reindexes all
     //$indexer->reindexRow($id); // or you can use reindexRow according to your need
     echo $indexerId;
     echo '<br>';
 echo 'Succesfully Reindex';

Or for flush the Cache and Reindexing execute shell commands in php file.

$command = 'php /path-to-magento-root-directory/bin/magento cache:clean && php /path-to-magento-root-directory/bin/magento indexer:reindex && php /path-to-magento-root-directory/bin/magento cache:flush';
echo '<pre>' . shell_exec($command) . '</pre>'; 
 $indexer = $this->_objectManager->get('Magento\Framework\Indexer\IndexerRegistry')->get($indexerId);

try this code and make sure $indexerId is your selected index id.

  • I edited my post. – Gediminas May 13 '17 at 12:46

This errors type occurs when you are extending a class and the child class construct does not match the parent class construct

Probably the parent class expects


where as you have passed



Why are you using object manager try to place this code loop and try

   foreach ($ids as $indexerId) {
       $idx = $this->indexerFactory->create()->load($indexerId);
  • Please, check my code. – Gediminas May 13 '17 at 12:47
  • Do you get the same error still ? Have you tried clearing var/generation folder ? – Priyank May 13 '17 at 12:52
  • Yes, the same error. – Gediminas May 13 '17 at 12:59
  • Try using IndexerFactory and remove var/cache and var/generation – Priyank May 13 '17 at 13:03
  • Sorry for my poor knowledge, I am newbie at OOP. I've changed to your loop, but no difference. Cache cleaned. – Gediminas May 13 '17 at 16:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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