If you want customer grid reindex programmatically, you can use the below step.
1. Create file menu.xml on app/code/Magemonkeys/CustomerReindex/etc/adminhtml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Backend:etc/menu.xsd">
<menu>
<add id="Magemonkeys_CustomerReindex::customer_reindex" title="Eagle Rocket" translate="title" module="Magemonkeys_CustomerReindex"
sortOrder="50" resource="Magemonkeys_CustomerReindex::manage_customer_reindex" parent="Magento_Customer::customer"/>
<add id="Magemonkeys_CustomerReindex::customer_manage" title="Customer Reindex" translate="title" module="Magemonkeys_CustomerReindex"
sortOrder="10" parent="Magemonkeys_CustomerReindex::customer_reindex" action="customerreindex/customerindexer/index" resource="Magemonkeys_CustomerReindex::manage_customer_reindex"/>
</menu>
</config>
2. Create file routes.xml on app/code/Magemonkeys/CustomerReindex/etc/adminhtml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
<router id="admin">
<route id="customerreindex" frontName="customerreindex">
<module name="Magemonkeys_CustomerReindex" before="Magento_Backend" />
</route>
</router>
</config>
3. Create file Index.php on app/code/Magemonkeys/CustomerReindex/Controller/Adminhtml/Customerindexer
<?php
namespace MagemonkeysCustomerReindexControllerAdminhtmlCustomerindexer;
class Index extends MagentoBackendAppAction
{
protected $indexFactory;
protected $indexCollection;
protected $resultRedirectFactory;
public function __construct(
MagentoBackendAppActionContext $context,
MagentoIndexerModelIndexerFactory $indexFactory,
MagentoIndexerModelIndexerCollectionFactory $indexCollection,
MagentoFrameworkControllerResultRedirectFactory $resultRedirectFactory,
MagentoFrameworkMessageManagerInterface $messageManager
)
{
parent::__construct($context);
$this->indexFactory = $indexFactory;
$this->indexCollection = $indexCollection;
$this->resultRedirectFactory = $resultRedirectFactory;
$this->_messageManager = $messageManager;
}
public function execute()
{
$indexerCollection = $this->indexCollection->create();
$indexids = $indexerCollection->getAllIds();
$indexid = 'customer_grid';
$indexidarray = $this->indexFactory->create()->load($indexid);
$indexidarray->reindexAll($indexid);
$resultPage = $this->resultRedirectFactory->create();
$resultPage->setPath('customer/index/index');
$this->_messageManager->addSuccess(__("Customer Reindex Successfully."));
return $resultPage;
}
}
4. Output


