0

If I deactivate the plugin, the database table does not get removed. It is removed only when i delete the plugin. Here's my code:

<?php
register_deactivation_hook( __FILE__, 'my_plugin_remove_database' );
function my_plugin_remove_database() {
     global $wpdb;
     $table_name = $wpdb->prefix . 'sandbox';
     $sql = "DROP TABLE IF EXISTS $table_name";
     $wpdb->query($sql);
     delete_option("my_plugin_db_version");
}   ?>
3

1 Answer 1

1
function my_plugin_remove_database()
{
    global $wpdb;
    $db_table_name = $wpdb->prefix . 'sandbox';  // table name
    $sql = "DROP TABLE IF EXISTS $db_table_name";
    $rslt=$wpdb->query($sql);
}

register_deactivation_hook( __FILE__, 'my_plugin_remove_database' );
1
  • Can you explain what you've changed please? At first glance you're just capturing the result of the query in a variable but then doing nothing with it. (You could at least log errors in the error log even if we can't report success here - I'm not sure?) What were you trying to do?
    – Rup
    Commented Feb 24, 2021 at 10:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.