How to Delete the Default WordPress Themes for all Websites with WP-CLI

In this tutorial, I am going to show you how to remove the default WordPress themes for all websites on your server by using wp-cli.

Why remove the default WordPress themes?

Before starting, you might even wonder why we should remove the default WordPress themes. Here are a few reasons why webmasters tend to prefer to remove the default themes that ship by default with every WordPress installation:

Security

Default WordPress themes, such as Twenty Twenty-One, are often targeted by hackers because they are widely used. If these themes are not actively maintained or updated, they may have vulnerabilities that can be exploited. By removing unused default themes, you reduce the potential attack surface and enhance the overall security of your WordPress installation.

Free up server space

If you maintain lots of websites, removing the default themes can help increase available server space and potentially make room for more websites.

Reduce complexity

Having fewer themes installed makes your WordPress installation simpler and easier to manage. Unnecessary themes can clutter your dashboard and increase the risk of accidental theme activation or misconfiguration. Removing unused themes streamlines the theme selection process and decreases the chance of errors.

Improve Maintenance

Keeping your WordPress installation lean makes it easier to perform updates and maintenance tasks. When you have fewer themes, you spend less time managing updates, ensuring compatibility, and troubleshooting potential conflicts between themes and plugins.

Improve Backup creation process

When you create backups of your WordPress site, unnecessary themes are included in the backup files, making them larger. By removing unused themes, you can reduce the size of your backups, making the backup and restore processes more efficient.

Now that we have covered the reasons why you would want to get rid of the themes you do not use in you WordPress installation, lets see how we can actually handle it with wp-cli.

Why WP-CLI?

WP-CLI is a command-line tool that can help automate certain repetitive tasks. For example, you may use it to update WordPress core, themes and plugins, to install and activate plugins, to create and manage users and content, to do search and replace in the database and more. For the purpose of this tutorial, we will examine how to get rid of all the default themes in our WordPress installations that we do not use.

Remove all default WordPress themes

First of all, check if you have wp-cli installed on your web server. Some shared hosting accounts do not have wp-cli installed, so use a hosting provider like Hostarmada, which have wp-cli preinstalled on all their plans. For the purpose of this tutorial, I will assume that you have ssh access as a root user to an Ubuntu web server. Skip the next paragraph if you already have wp-cli installed. To know if you have it installed or not, ssh to your server and run the following command:

wp --info

Install WP-CLI

You can follow the official WordPress documentation on how to install wp-cli. Once you have authenticated with ssh to your web server, write the below command:

curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar

This will download the Phar build on your server, make it executable and place it in your PATH. Next, check if the script works:

php wp-cli.phar --info

If you want to just type wp instead of php wp-cli.phar, you need to make the file executable and move it to somewhere in your PATH. Run the following commands:

chmod +x wp-cli.phar
sudo mv wp-cli.phar /usr/local/bin/wp

Finally, type wp --info to make sure the above configuration works.

Delete default WordPress themes

Deleting a default theme with wp-cli is as easy as running the following command in the website root folder:

wp theme delete twentytwentytwo

However, if we have a lot of websites, we would need to find a way to loop through all the folders and execute the script for every website. Lets create an executable script. Use nano (or any other text editor) to create a bash file called batch-delete-themes.sh:

nano batch-delete-themes.sh

Add the following code in the script:

#!/bin/bash

# Path to the wwwroot folder
ROOT_PATH="/www/wwwroot"

# Loop through each website directory in wwwroot
for website_dir in "$ROOT_PATH"/*; do
    if [ -d "$website_dir" ]; then
            wp --path="$website_dir" theme delete twentytwentytwo
            wp --path="$website_dir" theme delete twentytwentythree
            wp --path="$website_dir" theme delete twentytwentyfour
    fi
done

Replace the ROOT_PATH with the folder where your websites reside. Also replace the default themes with the default themes that you have installed. Save the script with ctrl + X and the “y”, make it executable and move it to your scripts folder with the following commands (if you use Ubuntu, it is /usr/local/bin):

chmod +x batch-delete-themes.sh
sudo mv batch-delete-themes.sh /usr/local/bin/batch-delete-themes.sh

Finally, run the script from the ssh terminal:

batch-delete-themes.sh