Question

I am trying to drop the database table created when my custom plugin is activated. I am using basically the same code, just a drop query. However, the table won't drop!

I have confirmed the following:

  • The WP Database User has privileges to drop tables (I confirmed by running sql query in workbench)
  • The query is being called and is correct (I used 'die($sql)' to output the query, then ran it in workbench)

    function my_plugin_remove_database() {
         global $wpdb;
         $table_name = $wpdb->prefix . "my_plugin_table";
         $sql = "DROP TABLE IF EXISTS $table_name;";
         //die($sql);
         require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
         dbDelta( $sql );
         delete_option("my_plugin_db_version");
    }
    register_deactivation_hook( __FILE__, 'my_plugin_remove_database' );
    
Was it helpful?

Solution

Use $wpdb->query() instead of dbDelta()

function my_plugin_remove_database() {
     global $wpdb;
     $table_name = $wpdb->prefix . "my_plugin_table";
     $sql = "DROP TABLE IF EXISTS $table_name;";
     $wpdb->query($sql);
     delete_option("my_plugin_db_version");
}

register_deactivation_hook( __FILE__, 'my_plugin_remove_database' );

dbDelta() does not supported DROP TABLE query.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top