Caution: This post was published in 2019 and may contain information, techniques, or code examples that are no longer current. Please double-check official documentation and modern best practices before using anything from this article.
April 2019

Magento Code: Magento 2 Indexer Script

This is a stand alone script which can be called to reindex a Magento 2 Site

<?php
use Magento\Framework\App\Bootstrap;
include('app/bootstrap.php'); // Update as required, this line will work if placed in the Magento Root Directory
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$params = $_SERVER;
$bootstrap = Bootstrap::create(BP, $params);
$obj = $bootstrap->getObjectManager();
$state = $obj->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
$indexer = $obj->get('Magento\Indexer\Model\Indexer\CollectionFactory')->create();
$ids = $indexer->getAllIds();
?><div style='top:45%; position:absolute; width:75%; text-align:center; color:#666; font-weight:bold; font-size:1.8em; '><?php
?><span style='color:green'>&#9679; Reindexing... </span> <?php
foreach ($ids as $indexerId) {
$indexer = $obj->get('Magento\Framework\Indexer\IndexerRegistry')->get($indexerId);
$indexer->reindexAll($indexerId); // this reindexes all
echo '&block;';
}
echo ' Complete. ';
view raw indexer.php hosted with ❤ by GitHub

The script will create a unique instance of the Magento Object Manager and use it to loop through all the indexes in the registry. You shouldn't need to modify the script for any third party indexes that may be added by modules, as long as they have been properly registered.

More posts from Magento

Using WordPress as a static site generator

Static site generators have gained significant traction amongst developers, designers, and businesses seeking faster, more secure websites. Unlike traditional dynamic sites, which rely on a database to deliver content on…

Continue reading

Moving a WordPress Website with ACF and Custom Post Types to Brightspot CMS

Migrating a website from WordPress to Brightspot CMS can seem daunting, particularly when the WordPress installation relies heavily on Advanced Custom Fields and Custom Post Types . Both ACF amd…

Continue reading

What's going on between WordPress and WP Engine?

The disagreement between WordPress and WP Engine has sparked considerable debate within the WordPress community and could have important implications for users of the WordPress content management system (CMS). WP…

Continue reading

Combining Laravel with WordPress

Combining Laravel with WordPress offers a unique and powerful approach to web development, blending the strengths of both platforms to create highly efficient, flexible, and scalable applications. Laravel, a modern…

Continue reading

you