Skip to content

Instantly share code, notes, and snippets.

@colinmollenhour
Created May 17, 2012 00:50
Show Gist options
  • Star 72 You must be signed in to star a gist
  • Fork 21 You must be signed in to fork a gist
  • Save colinmollenhour/2715268 to your computer and use it in GitHub Desktop.
Save colinmollenhour/2715268 to your computer and use it in GitHub Desktop.
Simplified cache cleaning script for production updates.
<?php
/**
* Set global/skip_process_modules_updates to '1' in app/etc/local.xml and
* then use this script to apply updates and refresh the config cache without
* causing a stampede on the config cache.
*
* @author Colin Mollenhour
*/
umask(0);
ini_set('memory_limit','512M');
set_time_limit(0);
if(file_exists('app/Mage.php')) require 'app/Mage.php';
else require '../../app/Mage.php';
// Init without cache so we get a fresh version
Mage::app('admin','store', array('global_ban_use_cache' => TRUE));
echo "Applying updates...\n";
Mage_Core_Model_Resource_Setup::applyAllUpdates();
Mage_Core_Model_Resource_Setup::applyAllDataUpdates();
echo "Done.\n";
// Now enable caching and save
Mage::getConfig()->getOptions()->setData('global_ban_use_cache', FALSE);
Mage::app()->baseInit(array()); // Re-init cache
Mage::getConfig()->loadModules()->loadDb()->saveCache();
echo "Saved config cache.\n";
@Vinai
Copy link

Vinai commented Dec 19, 2012

Please be aware that the global_ban_use_cache option is only available since Magento 1.6, which means this (very neat) gist will only work for upgrades to Magento 1.6 (1.11 EE) or newer.

@clockworkgeek
Copy link

It might also be necessary to recompile before applying updates. Something like this:

@include 'includes/config.php';
if (defined('COMPILER_INCLUDE_PATH')) {
    Mage::getModel('compiler/process')->run();
}

Copy link

ghost commented May 21, 2015

I've added an exit if the script isn't run on CLI and made an include that works when you place the script in the 'shell' folder:

<?php
/**
 * Set global/skip_process_modules_updates to '1' in app/etc/local.xml and
 * then use this script to apply updates and refresh the config cache without
 * causing a stampede on the config cache.
 *
 * @author Colin Mollenhour
 */
if(strtolower(php_sapi_name()) != 'cli'){
  exit;
}

umask(0);
ini_set('memory_limit','512M');
set_time_limit(0);

set_include_path(
    get_include_path() . PATH_SEPARATOR .
    dirname(dirname(__FILE__))
);

require_once('app/Mage.php');

// Init without cache so we get a fresh version
Mage::app('admin','store', array('global_ban_use_cache' => TRUE));

echo "Applying updates...\n";
Mage_Core_Model_Resource_Setup::applyAllUpdates();
Mage_Core_Model_Resource_Setup::applyAllDataUpdates();
echo "Done.\n";

// Now enable caching and save
Mage::getConfig()->getOptions()->setData('global_ban_use_cache', FALSE);
Mage::app()->baseInit(array()); // Re-init cache
Mage::getConfig()->loadModules()->loadDb()->saveCache();
echo "Saved config cache.\n";

NB The memory limit of 512 might be too low, you could always disable it (at your own risk) by setting it to -1.

@barbazul
Copy link

It is important to note that you will still need to refresh the cache in the traditional way after running this script as any layout, ddl, block and possible other types of cache will not be up to date to the recently deployed code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment