Using WP-CLI to Batch Autodelete Inactive WordPress Plugins

Keeping a clean and secure WordPress site is best practice. This means the database doesn’t have unnecessary bloat and all plugins are useful and up-to-date. A clean WordPress site is essential for making troubleshooting easy in case things ever go pear-shaped. Inactive plugins that are outdated and have security vulnerabilities are still dangerous for your WordPress site or WooCommerce store even if they are deactivated (source)!

If you manage a lot of WordPress sites or WooCommerce stores this will be very useful to clean up inactive plugins quickly and easily!

Using WP-CLI to Batch Delete Inactive WordPress Plugins

Here is the most basic WP-CLI plugin list command

wp plugin list

We get this output, it’s from my page builder test box on Kinsta 🙂

+----------------------+----------+-----------+----------+
| name                 | status   | update    | version  |
+----------------------+----------+-----------+----------+
| bb-plugin            | inactive | none      | 1.10.9.1 |
| tiny-compress-images | inactive | none      | 2.2.6    |
| cornerstone          | inactive | none      | 2.1.0    |
| divi-builder         | inactive | none      | 2.0.39   |
| elementor            | inactive | available | 1.8.9    |
| fusion-builder       | inactive | none      | 1.2.2    |
| fusion-core          | inactive | none      | 3.2.2    |
| js_composer          | inactive | none      | 5.3      |
| wp-file-manager      | inactive | none      | 1.8      |
| kinsta-mu-plugins    | must-use | none      | 2.0.6    |
+----------------------+----------+-----------+----------+

Now we can filter the plugin list with the --field and --status flags

wp plugin list --field=name --status=inactive

Notice how the kinsta-mu-plugins is no longer in the list

bb-plugin
tiny-compress-images
cornerstone
divi-builder
elementor
fusion-builder
fusion-core
js_composer
wp-file-manager

With the xargs command the --replace flag tells xargs to replace the % with the output from the wp plugin list command.

wp plugin list --status=inactive --field=name --allow-root | xargs --replace=% wp plugin delete % --allow-root

Here is the output showing each plugin being deleted

Deleted 'bb-plugin' plugin.
Success: Deleted 1 of 1 plugins.
Deleted 'cornerstone' plugin.
Success: Deleted 1 of 1 plugins.
Deleted 'divi-builder' plugin.
Success: Deleted 1 of 1 plugins.
Deleted 'elementor' plugin.
Success: Deleted 1 of 1 plugins.
Deleted 'fusion-builder' plugin.
Success: Deleted 1 of 1 plugins.
Deleted 'fusion-core' plugin.
Success: Deleted 1 of 1 plugins.
Deleted 'js_composer' plugin.
Success: Deleted 1 of 1 plugins.
Deleted 'wp-file-manager' plugin.
Success: Deleted 1 of 1 plugins.

Here is another version what will allow you to create an exception list of plugins that are inactive which should not be deleted. All other inactive plugins not in the exception list will be deleted.

#!/usr/bin/env bash
# purpose delete inactive WordPress plugins

WPPATH=""
WPFLAGS="--path=${WPPATH} --skip-plugins --skip-themes --allow-root"

PLUGINEXCEPTIONS=(
woocommerce-advanced-shipping-packages
woocommerce-gateway-amazon-payments-advanced
woocommerce-email-test
)

PLUGINLIST=($(wp plugin list --fields=name --status=inactive ${WPFLAGS}))

for PLUGIN in ${PLUGINLIST[@]}
do
if [[ ! " ${PLUGINEXCEPTIONS[@]} " =~ " ${PLUGIN} " ]]; then
    wp plugin delete $PLUGIN
fi
done

Sources

wp plugin list command
xargs options