delete tables from the database when deactivating a plugin
In this tutorial, we will discuss How to delete tables from the database when deactivating the WordPress plugin. when you delete a WordPress plugin then you may want to remove all data, files, and database tables that used in your plugin. when the plugin deactivates you can use the register_deactivation_hook()
hook to delete tables from the database. In this example, we want a database table ‘students’ will drop when the plugin deactivated.
Code
<?php
// Delete-table
function delete_students_table() {
global $wpdb;
$table_name $wpdb prefix.'students';
$sql = "DROP TABLE IF EXISTS $table_name";
$wpdb->query($sql);
delete_option("devnote_plugin_db_version");
}
register_deactivation_hook( __FILE__, 'delete_students_table' );
?>
Here, I create a function delete_students_table()
. Inside this function I use wordpress default $wpdb->query
to use manupulate the DROP TABLE in sql. And finally the deactivation register_deactivation_hook
hook and pass devnote_plugin_db_version function into the parameter.
query($sql);
delete_option(“devnote_plugin_db_version”);
}
register_deactivation_hook( __FILE__, ‘delete_students_table’ );
?>
please check this code here is before of prefix please use arrow sign.