9

I created a plugin and want to add a function to delete my tables from the database when a user deletes my plugin. I created a function that deletes tables from the DB when a user deactivates my plugin, but I don't want that. Here is the code:

// Delete table when deactivate
function my_plugin_remove_database() {
     global $wpdb;
     $table_name = "NestoNovo";
     $sql = "DROP TABLE IF EXISTS $table_name;";
     $wpdb->query($sql);
     delete_option("my_plugin_db_version");
}    
register_deactivation_hook( __FILE__, 'my_plugin_remove_database' );

As you can see, this function deletes tables when the plugin is deactivated, but I need to do that when the plugin is deleted.

18

You could do this using the WordPress uninstall.php support:

<?php
    if( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) exit();
    global $wpdb;
    $wpdb->query( "DROP TABLE IF EXISTS NestoNovo" );
    delete_option("my_plugin_db_version");
?>

This uninstall.php file is called when your plugin is deleted.

5

Enter code here:

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

You need to use register_uninstall_hook hook instead of register_deactivation_hook to delete tables from the database.

register_deactivation_hook fires when we deactivate a plugin and register_uninstall_hook fires when we want to remove/delete our plugin.

Please use this code if you have only one table:

function delete_plugin_database_table(){
    global $wpdb;
    $table_name = $wpdb->prefix . 'table_name';
    $sql = "DROP TABLE IF EXISTS $table_name";
    $wpdb->query($sql);
}

register_uninstall_hook(__FILE__, 'delete_plugin_database_table');

If you have more than two tables then you use this code:

function delete_plugin_database_tables(){
        global $wpdb;
        $tableArray = [   
          $wpdb->prefix . "table_name1",
          $wpdb->prefix . "table_name2",
          $wpdb->prefix . "table_name3",
          $wpdb->prefix . "table_name4",
       ];

      foreach ($tableArray as $tablename) {
         $wpdb->query("DROP TABLE IF EXISTS $tablename");
      }
    }

    register_uninstall_hook(__FILE__, 'delete_plugin_database_tables');

Reference Links:

https://developer.wordpress.org/reference/functions/register_uninstall_hook/ https://developer.wordpress.org/plugins/plugin-basics/uninstall-methods/

-1

I know that there's this hook called: register_deactivation_hook that you can use to do stuff when the plugin is deactivated. Take a look at the documentation and see if it is what you're looking for.

For instance:

**register_deactivation_hook**(__FILE__, 'sm_deactivation');
function myplugin_deactivation(){
/*
     Stuff
*/}
-3

Unfortunately, WordPress does not expose functionality to do that. It only supports the register_uninstall_hook hook. This hook is called when the user clicks on the uninstall link that calls for the plugin to uninstall itself. The link won't be active unless the plugin hooks into the action. see http://codex.wordpress.org/Function_Reference/register_uninstall_hook

and the register_deactivation_hook hook. What most plugin developer do is add an checkbox to the setting table with the use of get_option, update_option. When this option is checked, the data is removed.

This way, temporary deactivation does not reset the option table of your plugin.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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