Using WordPress ‘delete_theme()’ PHP function

The delete_theme() WordPress PHP function is used to remove a theme from your WordPress site.

Usage

Here’s a basic example of how to use the delete_theme() function.

delete_theme('my-theme');

In this example, the function will delete the theme with the stylesheet name ‘my-theme’.

Parameters

  • $stylesheet (string) – This is the name of the stylesheet of the theme that you want to delete.
  • $redirect (string) – This is an optional parameter. You can specify a URL to which the user should be redirected after the theme is deleted. By default, this value is empty (”).

More information

See WordPress Developer Resources: delete_theme()
The delete_theme() function was introduced in WordPress 2.8.0.

Examples

Delete a Theme

In this example, we are deleting the ‘twentyseventeen’ theme.

delete_theme('twentyseventeen');

Delete a Theme and Redirect

Here, we’re deleting a theme and then redirecting to another page.

delete_theme('my-theme', 'https://example.com');

Check if Theme Deletion was Successful

This example checks if the theme deletion was successful and prints a message accordingly.

if(delete_theme('twentyseventeen')) {
  echo 'Twenty Seventeen has been deleted.';
} else {
  echo 'Failed to delete Twenty Seventeen.';
}

Multiple Theme Deletion

Here, we’re deleting multiple themes in a single run.

$themes_to_delete = ['theme1', 'theme2', 'theme3'];

foreach($themes_to_delete as $theme) {
  delete_theme($theme);
}

Safe Theme Deletion

In this example, we first check if the theme exists before attempting to delete it. This is a safer approach to avoid errors.

$theme = 'twentyseventeen';

if (wp_get_theme($theme)->exists()) {
  delete_theme($theme);
}