Why my function in register_uninstall_hook from WP doesn't delete tables

I have this code in my WP plugin:

if (class_exists('mypluginname')) {
    $mypluginname = new mypluginname;
    register_activation_hook( __FILE__, array($mypluginname, 'activation'));
    register_uninstall_hook( __FILE__, array($mypluginname, 'uninstall'));
}

And I have this function in class:

function uninstall() {

    if (!defined('WP_UNINSTALL_PLUGIN')) {
        die;
    }

    global $wpdb;
    $wpdb->query("DROP TABLE IF EXISTS {$wpdb->prefix}my_x");
    $wpdb->query("DROP TABLE IF EXISTS {$wpdb->prefix}my_y");
    $wpdb->query("DROP TABLE IF EXISTS {$wpdb->prefix}my_z");

}

If I uninstall my plugin, tables in DB are not deleted. Why?

Solutions

Your class method "uninstall" must be a static class method! https://developer.wordpress.org/reference/functions/register_uninstall_hook/

Try it like this!?

class mypluginname{
    static function uninstall() {
        if (!defined('WP_UNINSTALL_PLUGIN')) {
            die;
        }

        global $wpdb;
        $wpdb->query("DROP TABLE IF EXISTS {$wpdb->prefix}my_x");
        $wpdb->query("DROP TABLE IF EXISTS {$wpdb->prefix}my_y");
        $wpdb->query("DROP TABLE IF EXISTS {$wpdb->prefix}my_z");
    }
}

if (class_exists('mypluginname')) {
    register_uninstall_hook( __FILE__, 'mypluginname::uninstall' );
}

Similar questions

How to develop one contact form with tables but remove the tables for mobile devices?
I have developed an contact form 7 form with tables in to put the column next to each other, it works great but on mobile it looks awful, does anyone know how I can remove the tables only for mobile ? for instance, on pc I want the column to be next to each other, like it is and on mobile I want it to be underneath each other. Image of the contact ...
insert into tables from another tables
Well, I trying insert into tables to another but I get this error; WordPress database error: [You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ''lcl_user_agent' (user_agent, hitcount, click_timestap) SELECT visite_useragent,' at line 1] This is my code; I want in...
Why does Wordpress have separate 'usersmeta' and 'users' SQL tables. Why not combine them?
Alongside the users table, Wordpress has a usersmeta table with the following columns Each user has 20 rows in the usersmeta table, regardless of whether or not the rows have a filled-in meta_value. That said, would it not be more efficient to add the always-present meta rows to the users table? I'm guessing that the information in the users table ...
Why are there multiple tables in WordPress, and why is it only reading the wrong/last one?
I have a database that's already been functioning well. I downloaded it, and now I'm trying to upload it to a new WordPress site. Once the database is successfully loaded, the site still gives the default "Hello World" post. I checked what's going on in the database, and there's wp_post, wp_2_post, wp_3_post, wp_4_post, and wp_5_post that include m...
Delete hook incompatibility with delete function in wordpress?
I have this action/hook: The above works great when I am deleting posts from the admin. However, when the wp_delete_post() is called, the above function isnt called. How can I make the action work with the delete function?
How can I add a delete function to this script to delete file on server
i have a script which loads all files within a specific directory folder into a viewable table. I have also been able to successfully get the date uploaded element integrated and am using the $iterator function as seen in the code below UPDATE Updated code to most recent and added full script for better understanding including CD001's suggestions S...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy .