Showing posts with label ALL Cache. Show all posts
Showing posts with label ALL Cache. Show all posts

Tuesday 9 January 2018

Clear configurable Swatches, Images, Flush Magento ALL Cache and cache files

ini_set('display_errors', 1);
require_once('../app/Mage.php');
Mage::app()->setCurrentStore(Mage::getModel('core/store')->load(Mage_Core_Model_App::ADMIN_STORE_ID));
/* Clear Cache */
try {
    $allTypes = Mage::app()->useCache();
    foreach($allTypes as $type => $cache) {
        Mage::app()->getCacheInstance()->cleanType($type);
    }
   
    /**
     * Flush cache storage
     */
    Mage::dispatchEvent('adminhtml_cache_flush_all');
    Mage::app()->getCacheInstance()->flush();
   
    /**
     * Flush all magento cache
     */
    Mage::app()->cleanCache();
    Mage::dispatchEvent('adminhtml_cache_flush_system');
   
   
} catch (Exception $e) {
    Mage::logException($e->getMessage());
}
try {
    /* cleanImages */
    Mage::getModel('catalog/product_image')->clearCache();
    Mage::dispatchEvent('clean_catalog_images_cache_after');
   
    /* cleanSwatches */
    Mage::helper('configurableswatches/productimg')->clearSwatchesCache();
    Mage::dispatchEvent('clean_configurable_swatches_cache_after');
   
    clearCstoreCacheDir('../var/cache');
   
    echo 'Success';
   
} catch (Mage_Core_Exception $e) {
    echo $e->getMessage();
} catch (Exception $e) {
    echo Mage::helper('adminhtml')->__('An error occurred while clearing the configurable swatches image cache.');
}
function clearCstoreCacheDir($dirPath) {
    if (!is_dir($dirPath)) {
        throw new InvalidArgumentException("$dirPath must be a directory");
    }
    if (substr($dirPath, strlen($dirPath) - 1, 1) != '/') {
        $dirPath .= '/';
    }
    $files = glob($dirPath . '*', GLOB_MARK);
    foreach ($files as $file) {
        if (is_dir($file)) {
            self::deleteDir($file);
        } else {
            unlink($file);
        }
    }
    rmdir($dirPath);
}