Magento maintenance script for IIS

Important Magento maintenance, now for IIS too: optimize the speed and performance of your Magento ecommerce webshop by carrying out important maintenance. Remove old MySQL database log files and Magento cache data on a regular basis.

Magento eCommerce Maintenance: Improve Webshop Speed & Performance

Clear Magento cache and MySQL log tables, perform regular Magento maintenance on IIS web servers for better performance. Magento Community Edition is a very popular ecommerce and webshop solution. And very bloated as we all know. Anywhere you run your Magento webshop, it’s important to perform maintenance. Carrying out maintenance on a regular basis optimizes Magento performance.

Two of such important Magento maintenance tasks are clearing out and emptying the MySQL database cache and log tables (such as log_visitor, log_url), and Magento’s file system cache directories in /var. Most scripts and solutions out there are for Linux- and Unix webservers only. I decided to modify a Magento maintenance script, to run on Windows Server and IIS too.

To optimize MySQL databases, it utilizes my MySQLi multi_query statement to optimize all MySQL tables in one statement.

Ask your hosting provider to schedule this script as a Windows Server scheduled task, for instance once a day, and you’ll notice a speed improvement of your Magento webshop. Next, add support for WinCache and your Magento webshop is very, very fast. Even on IIS!

Magento. Photo credit: wmcclure333 via Flickr

Clear magento cache and log tables

Disclamer: this script is provided “AS-IS” and should not be put into production without testing! Save the following PHP code as maintenance.php and upload it to your web-root.

 * maintenance.php:
 * IIS Magento Maintenance Script
 * Run this script to optimize your Magento ecommerce webshop's MySQL 
 * database, by performing regular maintenance. For instance, 
 * schedule a Windows scheduled task to run 
 * and 
 * on a daily basis
 * Other versions exist @
 * Modified 2014-07-25, Jan Reilink
 *  Converted ext/mysql statements to mysqli
 *  Added mysqli->multi_query to optimize all tables in one query
 * Modified 2014-07-01, Jan Reilink
 *  Fix for Windows IIS web servers, on which exec('rm -rf '.$dir) 
 *  doesn't function

if(isset($_GET['clean'])) {
    switch($_GET['clean']) {
        case 'log':
            // perform maintenance on MySQL database log tables
        case 'var':
            // Clears Magento cache in var directory
else {

function clean_log_tables() {
    $xml = simplexml_load_file('./app/etc/local.xml', NULL, LIBXML_NOCDATA);
    if(is_object($xml)) {
        $db['host'] = $xml->global->resources->default_setup->connection->host;
        $db['name'] = $xml->global->resources->default_setup->connection->dbname;
        $db['user'] = $xml->global->resources->default_setup->connection->username;
        $db['pass'] = $xml->global->resources->default_setup->connection->password;
        $db['pref'] = $xml->global->resources->db->table_prefix;
        // the following MySQL database tables will be trucated (cleared) 
        // during maintenance
        $tables = array(
        $mysqli = new mysqli($db['host'], $db['user'], $db['pass'], $db['name']);
        if($mysqli->connect_error) {
            die('Connect Error (' . $mysqli->connect_errno . ') '
                . $mysqli->connect_error);
        foreach($tables as $table) {
            @mysqli->query('TRUNCATE `'.$db['pref'].$table.'`');

        INTO @optimizecmd FROM information_schema.tables
        WHERE table_schema=database(); 
        PREPARE s1 FROM @optimizecmd; EXECUTE s1;";
    } else {
        exit('Unable to load local.xml file');

function clean_var_directory() {
   // the following website directories are emptied or removed
    $dirs = array(
    foreach($dirs as $dir) {

function delete_files($target) {
    if(is_dir($target)) {
        $files = glob($target . '*', GLOB_MARK);
        foreach($files as $file)

function default_action() {
    // Just return an empty 403 Forbidden response
    return http_response_code(403);
?>Code language: HTML, XML (xml)

Magento Maintenance Schedule URI’s

To perform automatic maintenance of your Magento webshop, to clear its cache, schedule the following URI’s:


for which you can use wget or curl.

This Magento maintenance script is tested with Magento and Magento without problems.

Jan Reilink

Hi, my name is Jan. I am not a hacker, coder, developer or guru. I am merely an application manager / systems administrator, doing my daily thing at Embrace - The Human Cloud. In the past I worked for clidn and Vevida. With over 20 years of experience, my specialties include Windows Server, IIS, Linux (CentOS, Debian), security, PHP, websites & optimization. I blog at