I m seeking code for reindexing and cache clear via external php
file in Magento 2
. but I'm not succeeding.
Can anyone help me to achieve this?
Any help will be appreciated.
Use this code:
<?php
$command = 'php /path-to-magento-root-directory/bin/magento cache:clean && php /path-to-magento-root-directory/bin/magento indexer:reindex';
echo '<pre>' . shell_exec($command) . '</pre>';
?>
It will do the task and will produce this output:
Cleaned cache types:
config
layout
block_html
collections
reflection
db_ddl
eav
customer_notification
full_page
config_integration
config_integration_api
translate
config_webservice
Design Config Grid index has been rebuilt successfully in 00:00:04
Customer Grid index has been rebuilt successfully in 00:00:08
Category Products index has been rebuilt successfully in 00:00:02
Product Categories index has been rebuilt successfully in 00:00:00
Product Price index has been rebuilt successfully in 00:00:07
Product EAV index has been rebuilt successfully in 00:00:05
Catalog Search index has been rebuilt successfully in 00:00:19
Stock index has been rebuilt successfully in 00:00:02
Catalog Rule Product index has been rebuilt successfully in 00:00:04
Catalog Product Rule index has been rebuilt successfully in 00:00:00
Assuming your file is in magento root folder. Add the below code.
error_reporting(1);
ini_set('max_execution_time', 0);
use \Magento\Framework\App\Bootstrap;
require __DIR__ . '/app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
//for reindex.
$IndexerFactory = $objectManager->get('Magento\Indexer\Model\IndexerFactory');
$CollectionFactory = $objectManager->get('Magento\Indexer\Model\Indexer\CollectionFactory');
$indexerCollection = $CollectionFactory->create();
$ids = $indexerCollection->getAllIds();
foreach ($ids as $id) {
$idx = $IndexerFactory->create()->load($id);
$idx->reindexAll($id); // this reindexes all
//$idx->reindexRow($id); // or you can use reindexRow according to your need
}
// for clear cache
$cacheTypeList = $objectManager->get('Magento\Framework\App\Cache\TypeListInterface');
$cacheFrontendPool = $objectManager->get('Magento\Framework\App\Cache\Frontend\Pool');
$types = array('config','layout','block_html','collections','reflection','db_ddl','eav','config_integration','config_integration_api','full_page','translate','config_webservice');
foreach ($types as $type) {
$cacheTypeList->cleanType($type);
}
foreach ($cacheFrontendPool as $cacheFrontend) {
$cacheFrontend->getBackend()->clean();
}
Warning: Cannot modify header information - headers already sent by (output started at
Commented
Jun 27, 2017 at 7:31
Backticks can be used in PHP to execute shell commands:
<?php
`/var/www/magento2/bin/magento cache:flush && /var/www/magento2/bin/magento indexer:reindex`;
You can prepend it with echo
to show the output in your browser.
However, note that executing shell scripts in your php scripts with either backticks, exec()
or shell_exec()
is considered bad practice.