In this tutorial, I will explain you how to manage index and cache management using REST API in Magento 2. When, we changing the data of product,category,catalog rule etc. we need to do re-index for update new data. When, we are managing our Magento store using REST API, the data in Magento updated using API. So, if you want to clean cache or need to re-index data using REST API, then follow this technical blog.
By use of this code, you can re-index indexer which will be invalid and cache will be cleaned.
In addition, you may like this :
- Magento 2 : Disable Payment Method For Certain Customer Groups
- Magento 2 : List of payment methods using command line
Now, follow this below steps :
Index Management
1) First of all, create registration.php file at app/code/RH/CustomApi/ and paste the below code :
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*
*/
/**
* Created By : Rohan Hapani
*/
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'RH_CustomApi',
__DIR__
);
2) Then, Create module.xml file at app/code/RH/CustomApi/etc/ and paste the below code :
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*
* Created By : Rohan Hapani
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="RH_CustomApi" setup_version="1.0.0" schema_version="1.0.0" />
</config>
3) After that, Create di.xml file at app/code/RH/CustomApi/etc/ folder and paste the below code :
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*
* Created By : Rohan Hapani
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
<preference for="RH\CustomApi\Api\UpdateIndex" type="RH\CustomApi\Model\GetInvalidIndexer" />
<preference for="RH\CustomApi\Api\CleanCache" type="RH\CustomApi\Model\GetRemainCache" />
</config>
4) Then, Create webapi.xml file at app/code/RH/CustomApi/etc/ folder and paste the below code :
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*
* Created By : Rohan Hapani
*/
-->
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../app/code/Magento/Webapi/etc/webapi.xsd">
<!-- For Index Management -->
<route url="/V1/getinvalidindex" method="GET">
<service class="RH\CustomApi\Api\UpdateIndex" method="getinvalidindex" />
<resources>
<resource ref="admin" />
</resources>
</route>
<!-- For Cache Management -->
<route url="/V1/getremaincache" method="GET">
<service class="RH\CustomApi\Api\CleanCache" method="getremaincache" />
<resources>
<resource ref="admin" />
</resources>
</route>
</routes>
5) After that, Create interface file UpdateIndex.php file at app/code/RH/CustomApi/Api/ folder and paste the below code :
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*
*/
/**
* Created By : Rohan Hapani
*/
namespace RH\CustomApi\Api;
interface UpdateIndex
{
/**
* @api
* @param
* @return array
*/
public function getinvalidindex();
}
6) Then, Create GetInvalidIndexer.php file at app/code/RH/CustomApi/Model/ folder and paste the below code :
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*
*/
/**
* Created By : Rohan Hapani
*/
namespace RH\CustomApi\Model;
use RH\CustomApi\Api\UpdateIndex;
class GetInvalidIndexer implements UpdateIndex
{
/**
* @var \Magento\Indexer\Model\Processor
*/
protected $processor;
/**
* @param \Magento\Indexer\Model\Processor $processor
*/
public function __construct(
\Magento\Indexer\Model\Processor $processor
) {
$this->processor = $processor;
}
public function getinvalidindex()
{
$output = [];
$invalid_index = $this->processor->reindexAllInvalid();
if ($invalid_index)
{
$this->processor->reindexAllInvalid();
}
$inv_ind = $this->processor->reindexAllInvalid();
if (!$inv_ind)
{
$response = [
"code" => "200",
"message" => "Indexers are re-index successfully.",
];
}
else
{
$response = [
"code" => "300",
"message" => "Please re-index agian.",
];
}
$output[] = $response;
return $output;
}
}
Cache Management
7) After that, Create interface file CleanCache.php file at app/code/RH/CustomApi/Api/ folder and paste the below code :
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*
*/
/**
* Created By : Rohan Hapani
*/
namespace RH\CustomApi\Api;
interface CleanCache
{
/**
* @api
* @param
* @return array
*/
public function getremaincache();
}
8) In Last, Create GetRemainCache.php file at app/code/RH/CustomApi/Model/ folder and paste the below code :
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*
*/
/**
* Created By : Rohan Hapani
*/
namespace RH\CustomApi\Model;
use RH\CustomApi\Api\CleanCache;
class GetRemainCache implements CleanCache
{
/**
* @var \Magento\Framework\App\Cache\TypeListInterface
*/
protected $cacheTypeList;
/**
* @param \Magento\Framework\Model\Context $context
* @param \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList
*/
public function __construct(
\Magento\Framework\Model\Context $context,
\Magento\Framework\App\Cache\TypeListInterface $cacheTypeList
) {
$this->_cacheTypeList = $cacheTypeList;
}
public function getremaincache()
{
$remainCache = $this->_cacheTypeList->getInvalidated();
if ($remainCache)
{
foreach ($remainCache as $key => $value)
{
$this->_cacheTypeList->cleanType($key);
}
$valid = [
"code" => "200",
"message" => "Cache clean successfully",
];
}
else
{
$valid = [
"code" => "300",
"message" => "Already cleaned cache.",
];
}
return $valid;
}
}
Now, For execute API output :
Index Management
- URL : BASE_URL/rest/V1/getinvalidindex
- Method : GET
- Headers :
- Authorization => Bearer “your access token”
- Content-Type => application/json
Cache Management
- URL : BASE_URL/rest/V1/getremaincache
- Method : GET
- Headers :
- Authorization => Bearer “your access token”
- Content-Type => application/json
In conclusion, you just need to execute these above URL by postman and clean cache and re-index indexer by Magento 2 REST API.
I hope this blog will helpful for easily understand about how to manage index and cache management using Magento 2 REST API. In case, I missed anything or need to add some information, always feel free to leave a comment in this blog, I’ll get back with proper solution