Autodelete Unused Inactive WordPress Themes with WP-CLI

WordPress by default comes with a theme installed so you can get started. The default WordPress theme name generally is named after the year so like this year it is twentynineteen. The default theme is also very important for troubleshooting purposes to make sure that any conflict you may be experiencing on your site with a plugin isn’t due to the theme or theme version you are using.

When you upgrade to new WordPress versions it will actually install the new default theme if one is available because having it around as a troubleshooting tool is so essential. These old themes can accumulate quite a bit so in this post we are going to delete the inactive WordPress themes on your site with WP-CLI. I also included a script to let you set an exception of themes you want to keep for troubleshooting purposes that you can adjust as you wish.

Autodelete Unused Inactive WordPress Themes with WP-CLI

You can list all of the WordPress themes installed with this command

wp theme list --allow-root

You will see a list like this showing if updates are available, the current version and the status of the installed WordPress themes

+-----------------+----------+--------+---------+
| name            | status   | update | version |
+-----------------+----------+--------+---------+
| twentyfifteen   | inactive | none   | 2.4     |
| twentyfourteen  | inactive | none   | 2.6     |
| twentynineteen  | inactive | none   | 1.3     |
| twentyseventeen | inactive | none   | 2.1     |
| twentysixteen   | inactive | none   | 1.9     |
+-----------------+----------+--------+---------+

If you want to list only the inactive themes you can use the --field=name option to only display the theme names

wp theme list --status=inactive --field=name --allow-root

Now you will only see the WordPress theme names

+-----------------+
| name            | 
+-----------------+
| twentyfifteen   |
| twentyfourteen  |
| twentynineteen  |
| twentyseventeen |
| twentysixteen   |
+-----------------+

Using the handy xargs command we can automatically delete all of the themes that are inactive

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

The script below will keep the twentynineteen theme and delete all the other inactive themes on your site.
If you wish to keep multiple themes add the names on their own new line below twentynineteen.

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

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

THEMEEXCEPTIONS=(
twentynineteen
)

THEMELIST=($(wp theme list --fields=name --status=inactive ${WPFLAGS}))

for THEME in ${THEMELIST[@]}
do
if [[ ! " ${THEMEEXCEPTIONS[@]} " =~ " ${THEME} " ]]; then
    wp theme delete ${THEME} ${WPFLAGS}
fi
done

Here is a sample output of the script, notice the first line is because I did not remove the column header from the output. If you want that version skip over to the next script.

Warning: The 'name' theme could not be found.
Success: Theme already deleted.
Deleted 'twentyfifteen' theme.
Success: Deleted 1 of 1 themes.
Deleted 'twentyfourteen' theme.
Success: Deleted 1 of 1 themes.
Deleted 'twentyseventeen' theme.
Success: Deleted 1 of 1 themes.
Deleted 'twentysixteen' theme.
Success: Deleted 1 of 1 themes.

This version will remove the column header from the theme list command.

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

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

THEMEEXCEPTIONS=(
twentynineteen
)

THEMELIST=($(wp theme list --fields=name --status=inactive ${WPFLAGS} | sed '1d'))

for THEME in ${THEMELIST[@]}
do
if [[ ! " ${THEMEEXCEPTIONS[@]} " =~ " ${THEME} " ]]; then
    wp theme delete ${THEME} ${WPFLAGS}
fi
done

Sources

wp theme list command
xargs options