MonsterInsights Plugin Uninstall Database Cleanup with(out) WP-CLI

A number of WordPress plugins are notorious for not removing data after the plugin has been uninstalled – MonsterInsights is no exception. This can bloat the options database table. If you installed a couple of plugins to test on a site, then removed them, this can all lead to the options table growing or custom database tables being added but never removed. The result is a site database which now has unnecessary data in it for plugins that are no longer being used. In short, it’s easy to create a mess. Let’s help clean it up using WP-CLI, phpMyAdmin and Adminer.

MonsterInsights plugin is a commonly used plugin for Google Analytics integration on WordPress sites.

The plugin does have one major shortfall along with a number of other plugins, in that it will not remove any of the data it created, if the plugin has been deactivated and then deleted.

MonsterInsights Plugin Uninstall Database Cleanup with(out) WP-CLI

See the following example of the option values set by the MonsterInsight plugin on a site:

No we will go through each of the methods for cleaning up these options starting with WP-CLI.

Using WP-CLI to Delete MonsterInsights Options

MonsterInsights will still keep a number of options values bloating up the sites options database table, but WP-CLIis there to come to the rescue.

The options that the MonsterInsights will create can be removed by running the following WP-CLI commands:

# delete all monsterinsight option values
wp option delete monsterinsights_usage_tracking_config
wp option delete monsterinsights_over_time
wp option delete monsterinsights_db_version
wp option delete monsterinsights_settings_version
wp option delete monsterinsights_current_version
wp option delete monsterinsights_settings
wp option delete monsterinsights_site_tt
wp option delete monsterinsights_site_profile
wp option delete monsterinsights_version_upgraded_from
wp option delete monsterinsights_review
wp option delete monsterinsights_pageinsights_next_fetch
wp option delete monsterinsights_notices
wp option delete _amn_mi_last_checked
wp option delete _amn_mi-lite_last_checked

Currently, it is not possible to use WP-CLI to delete multiple options from a key file, but there is an issue for it. We can use a simple bash loop instead to automate this:

# list of Monsterinsights Options
OPTIONLIST=(
monsterinsights_usage_tracking_config
monsterinsights_over_time
monsterinsights_db_version
monsterinsights_settings_version
monsterinsights_current_version
monsterinsights_settings
monsterinsights_site_tt
monsterinsights_site_profile
monsterinsights_version_upgraded_from
monsterinsights_review
monsterinsights_pageinsights_next_fetch
monsterinsights_notices
_amn_mi_last_checked
_amn_mi-lite_last_checked
)

# loop through options and delete
for OPTION in ${OPTIONLIST[@]}
do
    wp option delete ${OPTION}
done

Delete MonsterInsights Options Using phpMyAdmin

If you are used to using a database manager with a GUI like phpMyAdmin then you can easily delete the option values that MonsterInsights plugin has left behind.

Select the wp_options database table and then select data, which will show you the records in the options database table.

In the left pane, find the wp_options table and click it. Then you will see the records in the options database table

Now click the Search tab and find the option_name field, enter monsterinsights% for the value.

Click Go in the bottom right

Click Check all to select all of the Monsterinsights options and click Delete.

Repeat the same process for _amn_mi% to search for those option_names.

Delete MonsterInsights Options Using Adminer WordPress Plugin

You can use a plugin like ARI Adminer.  Install ARI Adminer as you would any normal WordPress plugin.

In the admin sidemenu, go to ARI Adminer  > Run Adminer > New Window.

In the left pane, find the wp_options table and click it.  You will then see the records in the options database table on the right.

Now click the Search tab and find the option_name field, enter monsterinsights% for the value.

Click Select in the top right.

Click Modify to select all of the Monsterinsights options and then click Delete.

Repeat the same process for _amn_mi% to search for the additional leftover option_names from MonsterInsights.

Sources

https://developer.wordpress.org/cli/commands/option/delete/
https://wordpress.org/support/topic/complete-removal-of-the-plugin-3/
https://github.com/awesomemotive/google-analytics-for-wordpress/search?q=get_option&unscoped_q=get_option