Is any one having tried to create REST API so using custom API one can trigger re-indexing?

which method do i have to mention in webapi.xml file as this is purely AREA::ADMIN activitiy. currently tried with POST (create) and PUT (Update). Below is the webapi.xml

<route url="/V1/customapi" method="POST">
        <service class="Sark\CustomApi\Api\CustomRepositoryInterface" method="create"/>
             <resource ref="self"/>

Further, for controller do i have to create API folder and interface mandatory or it is optional.

Index pro-grammatically code is as follows:

$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);

It would be good if any other API is also there to do the same magic (except the direct custom script file).

Ref.: http://codetheatres.com/magento/creating-custom-api-in-magento2-using-rest-protocol/

1 Answer 1


Using API indexing is not a good Idea.

As Indexing run on the command line and it takes much usage on memory & time, So it, not a good idea.

If you want then change the indexing status of each indexing to pending state and Magento cron automatically do indexing as it was in pending. That is the best Solution.

Create di.xml at app/code/Sark/CustomApi/etc/ and where you will define a model class CustomRepository at app/code/Sark/CustomApi/Model/ which overrides Sark\CustomApi\Api\CustomRepositoryInterface.

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Sark\CustomApi\Api\CustomRepositoryInterface" type="Sark\CustomApi\Model\CustomRepository"/>

And CustomRepository.php code is


namespace Sark\CustomApi\Model;

class CustomRepository implements Sark\CustomApi\Api\CustomRepositoryInterface
     * @var \Magento\Framework\Indexer\IndexerRegistry
    private $indexerRegistry;
     * @var \Magento\Indexer\Model\ResourceModel\Indexer\State\CollectionFactory
    private $stateCollectionFactory;

    public function __construct(
        \Magento\Framework\Indexer\IndexerRegistry $indexerRegistry,
        \Magento\Indexer\Model\ResourceModel\Indexer\State\CollectionFactory $stateCollectionFactory
    ) {
        $this->indexerRegistry = $indexerRegistry;
        $this->stateCollectionFactory = $stateCollectionFactory;

    public function create()
        $indexerStates = $this->stateCollectionFactory->create();
        foreach ($indexerStates->getItems() as $indexerState) {
            $indexer = $this->indexerRegistry->get($indexerState->getIndexerId());
            if ($indexer->getId() && !$indexer->isWorking()) {
  • Accepted! many thanks for the answer. yes i agreed it will use much resources at server end and it should run on CLI. this will help a lot.
    – Softec
    Apr 21, 2019 at 7:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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