Cleaning up your WordPress Multisite instance with wp-cli

At some points in life, your WordPress Multisite will become too big and needs to be cleaned up, things like spam comments, revisions, transients... And of-course, you don't want to go blog by blog to clean up all those things. It may take days or weeks if you have > 1000 sites like mine. Here is your savior:

WP-CLI

WP-CLI is a command line tool which can help you manage your WordPress site flawlessly (:D). This blog post will show you how to use wp-cli to clean up your WordPress Multisite including:

1. Transients:

Create a shell script inside your WordPress dir root:

$ cd /path/to/wordpress/root
$ sudo nano clean_transients.sh


#!/bin/bash

for url in $(wp site list --field=url --allow-root)
do
  echo $url #used for progress purposes
  wp transient delete-all --url=$url --allow-root
done


$ sudo chmod +x clean_transients.sh
$ sudo ./clean_transients.sh --allow-root


2. Spam comments:

$ cd /path/to/wordpress/root
$ sudo nano clean_comments.sh

#!/bin/bash

for url in $(wp site list --field=url --allow-root)
do
  echo $url #used for progress purposes
  wp comment delete $(wp comment list --number=### --status=spam --field=ID --url=$url --allow-root) --force --url=$url --allow-root
done

$ sudo chmod +x clean_comments.sh
$ sudo ./clean_comments.sh


3. Revisions:

a. Install the following WordPress plugin:

$ cd /path/to/wordpress/root/wp-content/plugins
$ git clone https://github.com/trepmal/wp-revisions-cli.git

b. Network activate it in your Network Dashboard.

c. Create this shellscript and run:

$ cd /path/to/wordpress/root
$ sudo nano clean_revisions.sh

#!/bin/bash

for url in $(wp site list --field=url --allow-root)
do
  echo $url #used for progress purposes
  wp revisions clean --url=$url --allow-root
done

$ sudo chmod +x clean_revisions.sh



Cool!!!! \m/\m/\m/

Note that I use "--allow-root" directive here to run with root, if your wp-cli setup can run with your webroot user (www-data or administrator), remove it.


References: https://pressable.com/blog/2015/01/08/advanced-wp-cli-commands-comments-revisions/