I am writing my plugin's uninstall.php file and would like it to delete any terms that were created in the plugin's custom taxonomy.

In the plugin's uninstall.php file I am using this:

// Delete all custom terms for this taxonomy
$terms = get_terms('custom_taxonomy');
foreach ($terms as $term) {
    wp_delete_term( $term->ID, 'custom_taxonomy' );

The problem seems to be that the custom taxonomy isn't registered at this point, so get_terms returns an error for "invalid taxonomy", thus I can't delete the custom terms in this manner, which seems the most straightforward way to do so.

If the custom taxonomy isn't registered when uninstall.php is called, how can I have it so that my plugin is able to clean up it's custom data?

Any help is greatly appreciated.


  • 1
    I think the only solution is $wpdb. I'm no good with SQL, here's a starting point.
    – brasofilo
    Oct 18, 2013 at 4:47
  • @brasofilo Thank you. After your two comments and a little more digging, this is what I ended up doing. I try to avoid direct queries as much as possible. It'd be nice if there were a cleaner way to clean up custom taxonomy stuff upon uninstall. Would you move this to it's own answer so I can accept it?
    – edeneye
    Oct 18, 2013 at 16:59

4 Answers 4


Using the uninstall hook would be legitimate if it worked, but it dumps the same error as the uninstall file. It would have more chances of working as it loads the main plugin file to perform the uninstall function, but if the taxonomy is not registered anymore (and it is not, as we need to deactivate before uninstalling), WP cannot use get_terms.

Adapting the following function from this Stack Overflow answer should do the work:

function load_terms($taxonomy){
    global $wpdb;
    $query = 'SELECT DISTINCT 
                                `wp-cls`.wp_terms t 
                            INNER JOIN 
                                `wp-cls`.wp_term_taxonomy tax 
                             `tax`.term_id = `t`.term_id
                                ( `tax`.taxonomy = \'' . $taxonomy . '\')';                     
    $result =  $wpdb->get_results($query , ARRAY_A);
    return $result;                 

Based on input from brasofilo, here is what I ended up doing:

function delete_custom_terms($taxonomy){
    global $wpdb;

    $query = 'SELECT t.name, t.term_id
            FROM ' . $wpdb->terms . ' AS t
            INNER JOIN ' . $wpdb->term_taxonomy . ' AS tt
            ON t.term_id = tt.term_id
            WHERE tt.taxonomy = "' . $taxonomy . '"';

    $terms = $wpdb->get_results($query);

    foreach ($terms as $term) {
        wp_delete_term( $term->term_id, $taxonomy );

// Delete all custom terms for this taxonomy

Call your cleaning function when uninstalling your plugin through register_uninstall_hook

  • From WP: "Emphasis is put on using the 'uninstall.php' way of uninstalling the plugin rather than register_uninstall_hook." I've read this elsewhere as well, which is why I'm trying to use the uninstall.php file for all the uninstall procedures.
    – edeneye
    Oct 17, 2013 at 19:46
  • 1
    I think it wouldn't matter if the uninstall hook worked to solve this, but it doesn't, it dumps the same error as uninstall.php.
    – brasofilo
    Oct 18, 2013 at 4:46

I know this is 10 years old but as it came up on Google, I found , after some experiments a simpler solution.

Simply re-register the taxonomy in your uninstall code e.g.

    // register the term so we can then delete it
    register_taxonomy( 'my_custom_tax', 'my_cpt' );
    // remove all custom taxonomies
    $terms = get_terms( 'my_custom_tax', array( 'hide_empty' => false ) );
    foreach ( $terms as $term ) {
        wp_delete_term( $term->term_id, 'my_custom_tax' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.