Skip to content

Instantly share code, notes, and snippets.

@kraftb
Last active October 19, 2022 14:25
Show Gist options
  • Save kraftb/1851dd848fb1654c9f83 to your computer and use it in GitHub Desktop.
Save kraftb/1851dd848fb1654c9f83 to your computer and use it in GitHub Desktop.
Completely clear the cache of a TYPO3 instance (filesystem, database)

Script for clearing all TYPO3 caches

This script aims to clear all (yes. really all) caches being used by TYPO3. So the file caches in typo3temp and typo3conf/autoload, next the database caches in tables cf_* and eventual realurl caches. And finally any PHP caches like those from APC, xcache, or other PHP accelerators/compilers.

Simply install the script at some executable location on your webserver or local dev machine (like in $HOME/bin, etc.). Then use the script in the same way as "typo3-instance-sqlshell.sh" by pointing it to the path of the instance:

clearCache.sh /path/to/TYPO3/webroot

Of course you can call "clearCache.sh ." if you are directly in the TYPO3 webroot.

A nice way to install this script is to check out the GIST/github repository in your executable location ($HOME/bin) and create a symlink to the script. This way you can easily update to new versions of the script:

cd $HOME/bin
git clone https://gist.github.com/1851dd848fb1654c9f83.git
ln -s 1851dd848fb1654c9f83/clearCache.sh

git clone https://gist.github.com/a3becef65ad853243fe2.git
ln -s a3becef65ad853243fe2/typo3-instance-sqlshell.sh

Requirements

You also need "typo3-instance-sqlshell.sh" installed.

Installing on MAC OS

When installing this (those) scripts on MAC OS you will probably need to install a mysql client (for typo3-instance-sqlshell.sh) and the coreutil tool package to have "readlink" available:

brew install mysql
brew install coreutils
#!/bin/bash
# clearCache.sh
# Version 0.6 / (c) Bernhard Kraft <kraftb@think-open.at>
# This script is licensed under the GNU GPL v2.0
# This script needs "typo3-instance-sqlshell.sh" installed in
# a location pointed to by PATH environment variable.
# The script "typo3-instance-sqlshell.sh" can be found here:
# https://gist.github.com/kraftb/a3becef65ad853243fe2
# Get TYPO3 location
INSTANCEDIR="$1"
# 1. Are we on MAC OS?
OS="linux"
if [ `echo $OSTYPE | grep -c darwin` -gt 0 ]; then
OS="darwin"
fi
# Try to determine absolute INSTANCEDIR loaction using "realpath"
REALPATH_BIN=`which realpath 2> /dev/null`
if [ ! -x "$REALPATH_BIN" ]; then
REALPATH_BIN="/usr/bin/realpath"
fi
if [ ! -x "$REALPATH_BIN" ]
then
echo "Warning: No \"realpath\" installed. Can not determine absolute path of INSTANCEDIR."
echo "Using fallback method."
INSTANCEDIR="`dirname \"$INSTANCEDIR\"`/`basename \"$INSTANCEDIR\"`"
else
INSTANCEDIR=`$REALPATH_BIN "$INSTANCEDIR"`
fi
# Determine PHP executable
PHP_BIN=`which php5 2> /dev/null`
if [ -z "$PHP_BIN" ]
then
PHP_BIN=`which php 2> /dev/null`
fi
if [ -z "$PHP_BIN" ]
then
echo "Warning: No PHP binary found. Disabling OPcache clearing."
fi
# Determine which cache system(s) is/are enabled
if [ -n "$PHP_BIN" ]
then
APC_ENABLED=`echo "<?php phpinfo();" | $PHP_BIN | grep "apc.enabled" | grep "On"`
OPCACHE_ENABLED=`echo "<?php phpinfo();" | $PHP_BIN | grep "opcache.enable" | grep "On"`
XCACHE_ENABLED=`echo "<?php phpinfo();" | $PHP_BIN | grep "xcache.cacher" | grep "On"`
fi
# Output usage message
function usage {
printf "Usage: `basename $0` [pathToTYPO3-Instance]\n\n"
printf "pathToTYPO3-Instance must be a directory\n\n"
exit 1
}
# ---------------- Check for TYPO3 webroot -----------------------
if [ ! -d "$1" ]
then
usage
fi
if [ ! -d "$1/typo3conf" ]
then
usage
fi
if [ ! -d "$1/typo3temp" ]
then
usage
fi
if [ ! -d "$1/fileadmin" ]
then
usage
fi
# ---------------- Clear file caches ---------------------------------
rmrf_safe() {
## Try to "delete" DELPATH as atomically as possible by renaming
## the existing directory and then recreating it
DELPATH="$1"
# Path does not exist
if [ ! -e "$DELPATH" ]; then
return 0
fi
echo $DELPATH
if [ $OS == "darwin" ]
then
OWNER_GROUP=`stat -f %u:%g "$DELPATH"`
ACCESS=`stat -f %A "$DELPATH"`
else
OWNER_GROUP=`stat --format=%u:%g "$DELPATH"`
ACCESS=`stat --format=%a "$DELPATH"`
fi
rm -rf "$DELPATH-old"
mv "$DELPATH" "$DELPATH-old" && \
mkdir "$DELPATH" && \
chown $OWNER_GROUP "$DELPATH" && \
chmod 0$ACCESS "$DELPATH"
## Clean up: Remove the renamed typo3temp directory
rm -rf "$DELPATH-old"
}
# Remove everything in typo3temp/*
DELPATH1="$INSTANCEDIR/typo3temp"
rmrf_safe "$DELPATH1"
# Remove everything in typo3conf/autoload/*
DELPATH2="$INSTANCEDIR/typo3conf/autoload"
rmrf_safe "$DELPATH2"
# ---------------- Clear database cache ---------------------------------
TABLES=`echo "show tables like 'cf_%';" | typo3-instance-sqlshell.sh "$INSTANCEDIR" | tail -n +2`
CLEARCMD=""
for i in $TABLES; do
CLEARCMD+="TRUNCATE $i;"
done
echo $CLEARCMD | typo3-instance-sqlshell.sh "$INSTANCEDIR"
# ---------------- Reset opcaches (apc, opcache, xcache) ----------------
if [ -n "$APC_ENABLED" ]
then
echo "<?php apc_clear_cache();" | $PHP_BIN
fi
if [ -n "$OPCACHE_ENABLED" ]
then
echo "<?php opcache_reset();" | $PHP_BIN
fi
if [ -n "$XCACHE_ENABLED" ]
then
echo "<?php xcache_clear_cache();" | $PHP_BIN
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment