10

I am looking for code that can refresh or flush the Magento2 cache via script.

It was so easy in Magento 1.x.

I am running Magento2 on WAMP server (window).

2

@denish,say by using cmd you can clear cache. But ur issue at php command line

In order to run php client as command in window you need to set php as environment available How to set the env variable for PHP?

After that you can run any of magento 2 cli command from cmd like

php bin/magento cache:clean
php bin/magento cache:flush
           Or
php bin/magento c:c
php bin/magento c:f

At going at your project location from cmd

  • like same what is the steps for magento 1. – zus Jun 27 at 9:08
21

The below code programmatically flushes cache. It worked fine for me.

Case 1: Outside Magento

use Magento\Framework\App\Bootstrap;
include('../app/bootstrap.php');
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();


try{
    $_cacheTypeList = $objectManager->create('Magento\Framework\App\Cache\TypeListInterface');
    $_cacheFrontendPool = $objectManager->create('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();
    }
}catch(Exception $e){
    echo $msg = 'Error : '.$e->getMessage();die();
}

Case 2: Inside Magento

public function __construct(
    Context $context,
    \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
    \Magento\Framework\App\Cache\Frontend\Pool $cacheFrontendPool
) {
    parent::__construct($context);
    $this->_cacheTypeList = $cacheTypeList;
    $this->_cacheFrontendPool = $cacheFrontendPool;
}


$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) {
    $this->_cacheTypeList->cleanType($type);
}
foreach ($this->_cacheFrontendPool as $cacheFrontend) {
    $cacheFrontend->getBackend()->clean();
}
12

Hardcoding the types is a bad idea. Instead you can use the same method used by the cache:flush and cache:clean commands. The cache manager class can also pull all the cache types for you, as done in the example below.

public function __construct(
    \Magento\Framework\App\Cache\Manager $cacheManager
) {
    $this->cacheManager = $cacheManager;
}

private function whereYouNeedToCleanCache()
{
    $this->cacheManager->flush($this->cacheManager->getAvailableTypes());

    // or this
    $this->cacheManager->clean($this->cacheManager->getAvailableTypes());
}
3

To add to denish's answer, you could write a little php script and place it into your magento root folder:

<?php
    $command = 'php bin/magento cache:clean && php bin/magento cache:flush';
    echo '<pre>' . shell_exec($command) . '</pre>';
?>

This will give you an output like:

Cleaned cache types:
config
layout
block_html
collections
reflection
db_ddl
eav
config_integration
config_integration_api
full_page
translate
config_webservice
Flushed cache types:
config
layout
block_html
collections
reflection
db_ddl
eav
config_integration
config_integration_api
full_page
translate
config_webservice

Please be sure you can actually excecute php from the command line, else this will be useless. For windows you have to make sure you have added the php.exe to your PATH in the Environment Variables. Please see http://willj.co/2012/10/run-wamp-php-windows-7-command-line/

  • it's showing nothing – Arunendra Feb 11 '16 at 12:42
  • 1
    For windows you have to make sure you have added the php.exe to your PATH in the Environment Variables. Please see willj.co/2012/10/run-wamp-php-windows-7-command-line – tecjam Feb 11 '16 at 12:45
  • If you are able to use shell_exec() for PHP, then your install is not secure. This function should be disabled in a live environment. – frustratedtech Dec 19 '16 at 17:43
2

You can flush or refresh all the cache using following commands

php bin/magento cache:clean   
php bin/magento cache:flush

I hope this will help you.

  • How to do it on window ? – Arunendra Feb 11 '16 at 12:21
  • @Arunendra, In CLI open magento root then enter to clear the cache php bin/magento cache:clean like this way to enter all commands. More info click on this link – Bojjaiah Feb 11 '16 at 12:27
  • like same what is the steps for magento 1 – zus Jun 27 at 9:09
1

1. Define constructor – pass

Magento\Framework\App\Cache\TypeListInterface

and

Magento\Framework\App\Cache\Frontend\Pool

to your file’s constructor as defined below :-

public function __construct(
    Context $context,
    \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
    \Magento\Framework\App\Cache\Frontend\Pool $cacheFrontendPool
) {
    parent::__construct($context);
    $this->_cacheTypeList = $cacheTypeList;
    $this->_cacheFrontendPool = $cacheFrontendPool;
}

2. Now add following code to the method where you want clear/flush cache :-

$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) {
    $this->_cacheTypeList->cleanType($type);
}
foreach ($this->_cacheFrontendPool as $cacheFrontend) {
    $cacheFrontend->getBackend()->clean();
}

I hope this will useful for you.:)

0

create a file named cacheflush.php and Upload your Magento root folder like public_html of httdocs folder. then yoursite.com/cacheflush.php It will work perfectly. If you have no CLI mod in your hosting no problem... just use this code ..it will reduce your time .

<?php

        use Magento\Framework\App\Bootstrap;

        require __DIR__ . '/app/bootstrap.php';

        $bootstrap = Bootstrap::create(BP, $_SERVER);

        $obj = $bootstrap->getObjectManager();

        $state = $obj->get('Magento\Framework\App\State');
        $state->setAreaCode('frontend');
        $k[0]='bin/magento';
        $k[1]='cache:flush'; // write your proper command like setup:upgrade,cache:enable etc...
        $_SERVER['argv']=$k;
        try {
            $handler = new \Magento\Framework\App\ErrorHandler();
            set_error_handler([$handler, 'handler']);
            $application = new Magento\Framework\Console\Cli('Magento CLI');
            $application->run();
        } catch (\Exception $e) {
            while ($e) {
                echo $e->getMessage();
                echo $e->getTraceAsString();
                echo "\n\n";
                $e = $e->getPrevious();
            }
        }
    ?>
0

this worked for me

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$cacheManager = $objectManager->create('Magento\Framework\App\Cache\Manager');
$cacheManager->flush($cacheManager->getAvailableTypes());

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.