Title: wp_ajax_delete_theme
Published: August 16, 2016
Last modified: February 24, 2026

---

# wp_ajax_delete_theme()

## In this article

 * [Description](https://developer.wordpress.org/reference/functions/wp_ajax_delete_theme/?output_format=md#description)
    - [See also](https://developer.wordpress.org/reference/functions/wp_ajax_delete_theme/?output_format=md#see-also)
 * [Source](https://developer.wordpress.org/reference/functions/wp_ajax_delete_theme/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/functions/wp_ajax_delete_theme/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/wp_ajax_delete_theme/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/functions/wp_ajax_delete_theme/?output_format=md#wp--skip-link--target)

Handles deleting a theme via AJAX.

## 󠀁[Description](https://developer.wordpress.org/reference/functions/wp_ajax_delete_theme/?output_format=md#description)󠁿

### 󠀁[See also](https://developer.wordpress.org/reference/functions/wp_ajax_delete_theme/?output_format=md#see-also)󠁿

 * [delete_theme()](https://developer.wordpress.org/reference/functions/delete_theme/)

## 󠀁[Source](https://developer.wordpress.org/reference/functions/wp_ajax_delete_theme/?output_format=md#source)󠁿

    ```php
    function wp_ajax_delete_theme() {
    	check_ajax_referer( 'updates' );

    	if ( empty( $_POST['slug'] ) ) {
    		wp_send_json_error(
    			array(
    				'slug'         => '',
    				'errorCode'    => 'no_theme_specified',
    				'errorMessage' => __( 'No theme specified.' ),
    			)
    		);
    	}

    	$stylesheet = preg_replace( '/[^A-z0-9_\-]/', '', wp_unslash( $_POST['slug'] ) );
    	$status     = array(
    		'delete' => 'theme',
    		'slug'   => $stylesheet,
    	);

    	if ( ! current_user_can( 'delete_themes' ) ) {
    		$status['errorMessage'] = __( 'Sorry, you are not allowed to delete themes on this site.' );
    		wp_send_json_error( $status );
    	}

    	if ( ! wp_get_theme( $stylesheet )->exists() ) {
    		$status['errorMessage'] = __( 'The requested theme does not exist.' );
    		wp_send_json_error( $status );
    	}

    	// Check filesystem credentials. `delete_theme()` will bail otherwise.
    	$url = wp_nonce_url( 'themes.php?action=delete&stylesheet=' . urlencode( $stylesheet ), 'delete-theme_' . $stylesheet );

    	ob_start();
    	$credentials = request_filesystem_credentials( $url );
    	ob_end_clean();

    	if ( false === $credentials || ! WP_Filesystem( $credentials ) ) {
    		global $wp_filesystem;

    		$status['errorCode']    = 'unable_to_connect_to_filesystem';
    		$status['errorMessage'] = __( 'Unable to connect to the filesystem. Please confirm your credentials.' );

    		// Pass through the error from WP_Filesystem if one was raised.
    		if ( $wp_filesystem instanceof WP_Filesystem_Base && is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->has_errors() ) {
    			$status['errorMessage'] = esc_html( $wp_filesystem->errors->get_error_message() );
    		}

    		wp_send_json_error( $status );
    	}

    	require_once ABSPATH . 'wp-admin/includes/theme.php';

    	$result = delete_theme( $stylesheet );

    	if ( is_wp_error( $result ) ) {
    		$status['errorMessage'] = $result->get_error_message();
    		wp_send_json_error( $status );
    	} elseif ( false === $result ) {
    		$status['errorMessage'] = __( 'Theme could not be deleted.' );
    		wp_send_json_error( $status );
    	}

    	wp_send_json_success( $status );
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-admin/includes/ajax-actions.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-admin/includes/ajax-actions.php#L4378)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-admin/includes/ajax-actions.php#L4378-L4441)

## 󠀁[Related](https://developer.wordpress.org/reference/functions/wp_ajax_delete_theme/?output_format=md#related)󠁿

| Uses | Description | 
| [delete_theme()](https://developer.wordpress.org/reference/functions/delete_theme/)`wp-admin/includes/theme.php` |

Removes a theme.

  | 
| [request_filesystem_credentials()](https://developer.wordpress.org/reference/functions/request_filesystem_credentials/)`wp-admin/includes/file.php` |

Displays a form to the user to request for their FTP/SSH details in order to connect to the filesystem.

  | 
| [WP_Filesystem()](https://developer.wordpress.org/reference/functions/wp_filesystem/)`wp-admin/includes/file.php` |

Initializes and connects the WordPress Filesystem Abstraction classes.

  | 
| [current_user_can()](https://developer.wordpress.org/reference/functions/current_user_can/)`wp-includes/capabilities.php` |

Returns whether the current user has the specified capability.

  | 
| [wp_get_theme()](https://developer.wordpress.org/reference/functions/wp_get_theme/)`wp-includes/theme.php` |

Gets a [WP_Theme](https://developer.wordpress.org/reference/classes/wp_theme/) object for a theme.

  | 
| [__()](https://developer.wordpress.org/reference/functions/__/)`wp-includes/l10n.php` |

Retrieves the translation of $text.

  | 
| [wp_unslash()](https://developer.wordpress.org/reference/functions/wp_unslash/)`wp-includes/formatting.php` |

Removes slashes from a string or recursively removes slashes from strings within an array.

  | 
| [esc_html()](https://developer.wordpress.org/reference/functions/esc_html/)`wp-includes/formatting.php` |

Escaping for HTML blocks.

  | 
| [check_ajax_referer()](https://developer.wordpress.org/reference/functions/check_ajax_referer/)`wp-includes/pluggable.php` |

Verifies the Ajax request to prevent processing requests external of the blog.

  | 
| [wp_send_json_error()](https://developer.wordpress.org/reference/functions/wp_send_json_error/)`wp-includes/functions.php` |

Sends a JSON response back to an Ajax request, indicating failure.

  | 
| [wp_send_json_success()](https://developer.wordpress.org/reference/functions/wp_send_json_success/)`wp-includes/functions.php` |

Sends a JSON response back to an Ajax request, indicating success.

  | 
| [wp_nonce_url()](https://developer.wordpress.org/reference/functions/wp_nonce_url/)`wp-includes/functions.php` |

Retrieves URL with nonce added to URL query.

  | 
| [is_wp_error()](https://developer.wordpress.org/reference/functions/is_wp_error/)`wp-includes/load.php` |

Checks whether the given variable is a WordPress Error.

  |

[Show 10 more](https://developer.wordpress.org/reference/functions/wp_ajax_delete_theme/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/functions/wp_ajax_delete_theme/?output_format=md#)

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/wp_ajax_delete_theme/?output_format=md#changelog)󠁿

| Version | Description | 
| [4.6.0](https://developer.wordpress.org/reference/since/4.6.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_ajax_delete_theme%2F)
before being able to contribute a note or feedback.