How to Properly Remove WordPress Themes from Your Website

Home » Snippets » How to Properly Remove WordPress Themes from Your Website
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

WordPress makes it easy to switch between themes and preview new ones before making them live. However, over time, you may accumulate a number of unused themes that clutter your site and take up valuable storage space. In this article, we’ll walk you through the steps to safely remove a theme in WordPress, so you can free up space and streamline your site. Let’s get started!

					function wpturbo_remove_theme() {
    // Set the slug of the theme you want to remove
    $theme_slug = 'twentytwenty';
    
    // Delete the theme folder
    WP_Filesystem();
    $theme_dir = get_theme_root() . '/' . $theme_slug;
    if (is_dir($theme_dir)) {
        $filesystem = WP_Filesystem();
        $filesystem->delete($theme_dir, true); 
    }
    
    // Remove the theme from the database and activate another theme
    $themes = wp_get_themes();
    if (isset($themes[$theme_slug])) {
        switch_theme($themes[get_option('template')]->stylesheet);
        $theme = wp_get_theme($theme_slug);
        $theme->delete();
    }
}
add_action('wp_ajax_wpturbo_remove_theme', 'wpturbo_remove_theme');
				

The code snippet defines a function called wpturbo_remove_theme() that removes a WordPress theme from the file system and the database. This function uses WP_Filesystem() to delete the theme folder, so you need to have the required permissions to delete files on your server.

The first step is to set the slug of the theme you want to remove. You can find the slug of your theme by looking at the folder name of your theme. For example, if your theme is located in the themes/twentytwenty folder, the theme slug would be ‘twentytwenty’. You should replace twentytwenty with the slug of the theme you want to remove.

// Set the slug of the theme you want to remove
$theme_slug = 'twentytwenty';

Next, the code checks if the theme directory exists before deleting it. This is a useful safety check because it prevents the function from accidentally deleting a non-existent folder. If the theme directory exists, the WP_Filesystem() function will be called to delete the theme folder.

$theme_dir = get_theme_root() . '/' . $theme_slug;
if (is_dir($theme_dir)) {
    $filesystem = WP_Filesystem();
    $filesystem->delete($theme_dir, true); 
}

The third step is to remove the theme from the database and activate another theme. The code gets all the installed themes using the wp_get_themes() function and checks if the theme with the specified slug exists.

If the theme exists, the switch_theme() function is called to activate another theme. This is necessary because you can’t delete an active theme. After activating another theme, the wp_get_theme() function is used to get the theme object of the theme you want to delete. Finally, the delete() method of the theme object is called to remove the theme from the database.

$themes = wp_get_themes();
if (isset($themes[$theme_slug])) {
    switch_theme($themes[get_option('template')]->stylesheet);
    $theme = wp_get_theme($theme_slug);
    $theme->delete();
}

Lastly, the function is hooked into the wp_ajax_wpturbo_remove_theme action so that you can call this function using an AJAX request.

That’s it! With this code snippet, you can safely and easily remove a WordPress theme from both the file system and the database.

Register an account to save your snippets or go Pro to get more features.