How to clear Transients on all sites in Multi Sites environment

I know we have the following command available in WP-CLI to remove transients

# Delete all transients
$ wp transient delete-all

However, my question is does it remove transients on all sites over multi sites? If not how can we remove transients on all sites?

Topic transient wp-cli Wordpress

Category Web


Because not all server work with pipe xargs syntax (| xargs), I prefer the other syntax, for in. Also, because it is more comprehensible and allow to chain commands like so :

for url in $(wp site list --field=url --url={dom.fr}); do
wp option get siteurl --url=$url;
wp transient delete --all --url=$url;
wp cache flush --url=$url;
wp rewrite flush --url=$url;
wp cron event run --due-now --url=$url;
done;

(for display purpose I have added newlines, please delete them when copying in command line to paste one line)

  1. List all multisite's sub-sites: wp site list --field=url --url={dom.fr}. Always precise the --url in multisite with your domain.extension. The parent site could not be the first one.
  2. Then, iterating on all these sites with something similar to a foreach loop.
  3. I firstly added more information with wp option get siteurl to know in the command line which site is currently used. Usefull when working with a ton of sub-sites.
  4. Then lunch all the commands I want against a website with WP CLI. In this example, I delete all transients, flush the cache, flush the rewrites, even lunch the cron. It is limited to the current website in the foreach loop due to the --url arg.

The output will be something like that:

https://site.docksal
Success: 6 transients deleted from the database.
Success: The cache was flushed.
Success: Rewrite rules flushed.
Success: Executed a total of 0 cron events.
https://en.site.docksal
Success: 4 transients deleted from the database.
Success: The cache was flushed.
Success: Rewrite rules flushed.
Success: Executed a total of 0 cron events.

For more WP CLI samples, see my gist https://gist.github.com/MaximeCulea/575e493a061359edbb12cefc3aa4c770#file-wp-cli-ms-cheatset-php-L38


You can run any command against all sites in a network by combining the wp site list and the | xargs:

wp site list --field=url | xargs -n1 -I % wp --url=% transient delete --all

Source: https://runcommand.io/to/run-wp-cli-command-wordpress-multisite/

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.