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 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' );
Modifies the database based on specified SQL statements.
By default, WordPress uses this class to instantiate the global $wpdb object, providing access to the WordPress database. The actual line that checks if table already exists or not is inside if statement. This statement return true if there is no table with the name $table_name and vice versa.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With