Search code examples
phpcachingtypo3

How to clear the cache in Typo3 from an external script?


I do have a PHP script, which is not an extension for Typo3. Now I would like to delete the whole Cache of Typo3 out of this script. How is that possible?


Solution

  • I found the solution myself and its actually pretty easy. I took a look into the class.t3lib_tcemain.php in the t3lib folder. There you've got the necessary commands to clear the cache. It also checks, if you have the cachingframework enabled. If so, you need to truncate a few other tables as well (Starts with cachingframework_cache_)

    It is basically:

    <?php
    
     require_once('./typo3conf/localconf.php');
    
     $conn = mysql_connect($typo_db_host, $typo_db_username, $typo_db_password);
     mysql_select_db($typo_db);
    
     // Clear Cache here
     mysql_query("TRUNCATE cache_treelist;");
     mysql_query("TRUNCATE cache_pagesection;");
     mysql_query("TRUNCATE cache_hash;");
     mysql_query("TRUNCATE cache_pages;");
     if($handle = opendir('./typo3conf')) {
        while (false !== ($file = readdir($handle))) {
            if(strpos($file, 'temp_CACHED_')!==false) {
                unlink('./typo3conf/'.$file);
            }
        }
        closedir($handle);
     }
    
    ?>