Lesson 5.3: Clearing caches from the command line
Deploy scripts, cron jobs and support staff without a browser session clear caches in a terminal. Two commands do
it: the long-standing bin/php/ezcache.php, and exp:cache, which shares its code with the Caches page and reports
PASS or FAIL. This lesson uses both, always looking before clearing.
At a glance
| Course | Site administration |
| Module | Module 5: Caches |
| Duration | 35 minutes (10 presenting, 25 exercise) |
| Level | Intermediate |
| Audience | Site administrators comfortable with a terminal |
| Prerequisites | Lesson 5.2; a terminal in the training installation's root |
Learning objectives
After this lesson, learners can:
- list the caches with their tags and sizes;
- clear a cache by id and by tag with
ezcache.php; - preview a clear with
exp:cache ... --dry-runand read itsPASSline; - read the status of the caches in front of PHP;
- clear Velocity's response cache and say when Velocity needs a restart.
Trainer's notes
How to present it
Source: Operating a site, parts 1 and 2, Caches, from a shell and Cache control from the console.
- Look first (3 minutes).
php bin/php/cache.php list --sizeslists every cache with its tag, state, directory and size;php bin/php/cache.php statusgives one screen for the caches in front of PHP. - ezcache.php (3 minutes).
--clear-id=<id>for one or several caches,--clear-tag=<tag>by tag,--clear-allfor everything;--list-tagsand--list-idsprint the valid names.--purgeremoves entries physically instead of marking them expired. - exp:cache (2 minutes). The same work with
PASS/FAILon the last line and--jsonfor monitoring;--dry-runshows what would be cleared, with sizes. - Velocity (2 minutes).
exp:velocity cache clearclears the response cache;exp:velocity restartmakes Velocity read changed settings. On a server,exp:velocity deploydoes every step of a deployment in the right order (Hosting and operations, lesson 4.1).
Common questions
Do I need --allow-root-user? Only when you run the command as the operating system root user; the scripts
refuse root otherwise. Better: run them as the user that owns the files.
--clear-id=ini did not pick up my setting. Use the tag: --clear-tag=ini also clears the global INI cache.
Are command-line clears recorded? Yes, one audit record per run, naming the operating system user. The Caches page shows them in its last-cleared column.
Pitfalls
- Running as root and leaving root-owned files. A clear as root can leave directories the web server cannot write.
Show
ls -l var/after a clear as root, if your lab permits. --clear-allin a cron job. It makes the site slow every time it runs. Clear the smallest thing, or nothing: publishing clears the affected views itself.- Wrong directory. The commands must run in the installation root (the directory with
index.php).
Hands-on exercise
In a terminal, in the root of your training installation:
List the caches with their sizes:
php bin/php/cache.php list --sizesList the valid tags and ids:
php bin/php/ezcache.php --list-tags php bin/php/ezcache.php --list-idsPreview what clearing everything would touch:
php bin/php/cache.php clear --all --dry-runClear only the INI caches by tag, and read the output:
php bin/php/ezcache.php --clear-tag=iniClear the template block and content caches by id:
php bin/php/ezcache.php --clear-id=template-block,contentClear by tag with
exp:cacheand read the last line:php bin/php/cache.php clear --tag=templateRead the status of the caches in front of PHP:
php bin/php/cache.php statusClear Velocity's response cache:
php bin/php/console exp:velocity cache clear --applyOpen Setup > Cache management in the browser and read the last-cleared column: which clears were made from a shell, and by which operating system user?
Expected result
- Step 3 lists the caches it would clear and changes nothing.
- Step 6 ends with a line starting
PASS. - Step 7 ends with
PASS status readand has one line each for the static cache, the HTTP cache, the query cache, Velocity and PHP. - Step 9 shows the shell clears with the learner's operating system user.
- The trainer checks by asking a learner which command they would use after a template change.
Quiz
- Which option of
ezcache.phpclears every cache carrying one tag? A.--clear-idB.--clear-tagC.--purgeD.--list-tags - What does
--dry-runwithexp:cachedo? - Why prefer
--clear-tag=inito--clear-id=iniafter a settings change? - Which command clears Velocity's response cache?
- When does a script need
--allow-root-user?
Answers
- B.
- It shows what would be cleared, with sizes, and changes nothing.
- The tag also clears the global INI cache; the id alone may leave old settings in use (Cache control from the console).
exp:velocity cache clear(orphp bin/php/cache.php velocity clear).- Only when it runs as the operating system root user.
Further reading
- Operating a site: the caches, part by part.
- Cache control from the console: every
exp:cachegroup and option. - Caches: the shell commands next to the page.
Related pages
- Module 5: Caches
- Site administration: course overview
- Trainers: start here
- Free documentation: this page is free to copy, translate and improve under the GNU FDL 1.3.
Become an Exponential Partner
Partners share their information, provide funding, contribute back and develop the products with us, and that is how Exponential grows. A partner website and dashboard are planned for mid-2027: please become a partner today to speed up our growth rate and land new features faster.