0

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.

0

3 Answers 3

3

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
4
  • it is working fine but after running this file i have to clear my browser cookie but why? Jun 27, 2017 at 7:40
  • It doesn't need to clear browser cookie. It just works in the same manner as the running the commands in shell.
    – Manish Joy
    Jun 27, 2017 at 7:43
  • I have tried this and found no such problem on my end.
    – Manish Joy
    Jun 27, 2017 at 7:43
  • ok it is working fine now.. Jun 27, 2017 at 7:46
1

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();
}
2
  • Ok let me try with this code.. Jun 27, 2017 at 7:24
  • Your code is not working properly. it is goes infinite after doing this. when i try to open anything. it gives me error like this.Warning: Cannot modify header information - headers already sent by (output started at Jun 27, 2017 at 7:31
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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