Delete tables from Database when deactivate WordPress plugin

While you delete a WordPress plugin then you may
want to remove all your database tables that used in your plugin.

There is a hook in WordPress register_deactivation_hook() . It will
work when plugin deactivate. And we want a database table ‘people’ will drop when plugin deactivated. Here is the code for that.


<?php
// Delete table
function delete_people_table() {
global $wpdb;
$sql = "DROP TABLE IF EXISTS people";
$wpdb->query($sql);
delete_option("my_plugin_db_version");
}
register_deactivation_hook( __FILE__, 'delete_people_table' );
?>

I create a function delete_people_table(). Inside this function
I use $wpdb->query for manupulate the DROP TABLE sql. Finally use the
deactivation hook and pass ‘delete_people_table’ function into the parameter.

Hope this tutorial helps.

One thought on “Delete tables from Database when deactivate WordPress plugin”

Leave a Reply

Your email address will not be published. Required fields are marked *