2

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?

3
  • My customer has an import script, which imports several things, which are not reflectable to an extension. He does not want to login and clear the cache every time, the cron job starts. Thats not a solution at all. Commented Mar 12, 2013 at 12:33
  • 1
    @Bhumi There is a lot of cases when we need such automatic behavior. Commented Mar 14, 2013 at 13:30
  • Why is it a prerequisite to do it in a PHP script? If you explain what you would like to achieve and why (e.g. describe use case), you might get better answers. (Though this question is old, it still turns up in search results and it might be helpful for others to update and improve question and answers) Commented Jan 19, 2022 at 13:02

5 Answers 5

6
  1. install the TYPO3 Extension cleartypo3cache
  2. create a tool and a keyboard shortcut in PhpStorm 4 to trigger cleartypo3cache
  3. SSH access with passwordless pubkey authentication when pushing to a remote host.

Install Extension "cleartypo3cache" and create the BE user "_cli_cleartypo3cache" and add the following TSconfig:

options.clearCache.all=1
options.clearCache.pages=1

Now test if cache is cleared:

$ cd /path/tp/typo3-site/
$ php typo3/cli_dispatch.phpsh cleartypo3cache all

If your webserver is on localhost, you are lucky because you don't need this shell script. If your webserver is on a remote host, you need an additional wrapper script. This is because PhpStorm does not provide an environment variable for the remote host directory. You have to set this directory statically for each project in the wrapper script:

#!/bin/sh

TYPO3_SITE_PATH="/path/to/typo3-site"
USER="alice"
HOST="example.com"

/usr/bin/ssh $USER@$HOST '/usr/bin/php $TYPO3_SITE_PATH/typo3/cli_dispatch.phpsh cleartypo3cache all'

Save this file in your project file directory into .idea/clear-typo3-cache.sh and make it executable:

$ chmod 755 .idea/clear-typo3-cache.sh

PhpStorm External Tools You need to create an "external tool" in PhpStorm to be able to clear cache.

  1. Go to PhpStorm-->Settings-->External Tools-->Add...
  2. Give your tool a name and a group, e.g. "Deployment" -> "Clear TYPO3 Cache"
  3. Deactivate checkbox "Open Console" and "Menu->Search Results"

Remote host scenario

Add the following line to "Programm:"

$ProjectFileDir$/.idea/clear-typo3-cache.sh

Localhost scenario

Add this line to "Program:"

$PhpExecutable$

Add this line to "Parameters:"

$ProjectFileDir$/typo3/cli_dispatch.phpsh cleartypo3cache all

You need to have a PHP interpreter configured in PhpStorm-->Settings-->PHP to use $PhpExecutable$. Alternatively you can use /usr/bin/php
(source: t3node.com)

PhpStorm Keymap I suggest to use the same key binding as you use for saving or remote host uploading:

Go to PhpStorm-->Settings-->Keymap

For remote host scenario, navigate to: Main menu-->Tools-->Deployment-->Upload to Default Server. Notice the existing shortcut. If you don't have one for that, create a new one (I use ALT+SHIFT+U) For the localhost scenario, just use Ctrl+S (Main menu-->File-->Save All).

  1. Now navigate to the External Tool you have created (e.g. External Tools-->Deployment->Clear TYPO3 Cache)
  2. Right click "Add Keyboard Shortcut"
  3. Create the particular shortcut in "First Stroke"

Now PhpStorm will warn you that the shortcut is already in use for a different command. That's fine, it's exactly what we want to have.

That's it. Your TYPO3 caches are always cleared when you hit save or upload on your keyboard.

adapted from t3node

Sign up to request clarification or add additional context in comments.

2 Comments

Thats a very detailed answer, but there must be another way, without installing any extensions and such stuff. For example in Magento, you just need to include the Mage-Loader and then you've got the necessary methods available.
Why are you using PhpStorm to iniate a cache flush? I can see a possible usecase in development, but nowadays there are other ways and more built in ways to do this. This answer seems overly complicated but I may not understand the usecase correctly.
5

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);
 }

?>

5 Comments

There's a missing / between typo3conf and $file at the unlink & you should flush the typo3temp folder as well.
I added the slash, that was a mistake, sorry about it.
what version of typo3 you're using ??
question is whether you always want to flush all caches - if custom solution is created anyway, this could be made more flexible. Also, you can flush caches for specific pages by using the cache tags. docs.typo3.org/m/typo3/reference-coreapi/main/en-us/ApiOverview/…
4

TYPO3 >= 12

./vendor/bin/typo3 cache:flush

TYPO3 >= 7

From TYPO3 7 on you can install Helmut Hummels Extension typo3_console. Then you can clear the cache like (for composer installations):

./vendor/bin/typo3cms cache:flush

https://extensions.typo3.org/extension/typo3_console/

https://github.com/TYPO3-Console/TYPO3-Console

TYPO3 6.x

first initialize the Service in your Class

/**
 * @var Tx_Extbase_Service_CacheService
 */
protected $cacheService;

/**
 * @param Tx_Extbase_Service_CacheService $cacheService
 * @return void
 */
public function injectCacheService(Tx_Extbase_Service_CacheService $cacheService) {
    $this->cacheService = $cacheService;
}

in your function just call

$this->cacheService->clearPageCache($pids);

while $pids is an integer (for single page) or array of integers (multiple pages)

see: http://typo3.org/api/typo3cms/class_t_y_p_o3_1_1_c_m_s_1_1_extbase_1_1_service_1_1_cache_service.html

In TYPO3 since 4.5 (I think) its a static method so you have just to call

Tx_Extbase_Utility_Cache::clearPageCache($pids);

in your controller.

3 Comments

or use @inject annotation instead the inject method
I think your "Update" answer is the right way today for Typo3 v7++. Maybe you should update it as a new answer instead of update your old answer. I call it within an ansible command like 'docker exec -it typo3 php vendor/composer/bin/typo3cms cache:flush' and it works as expected.
I changed the order - to get attention to the updated answer.
1

In FLOW3 there is a possibility to do such stuff, as far as I know with TYPO3 v.4.x You have no such default CLI option, so You should use or You own script, or use such extensions as cleartypo3cache or Cli Cleaner.

Also I made a bash script to clean cache tables of Your dB : https://gist.github.com/fedir/5162747

Comments

1

in typo3 6.x extbase its simple.

Edit : clearPageCache is not static then you need to create object of CacheService

TYPO3\CMS\Extbase\Service\CacheService::clearPageCache(pageUid);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.