Delete tables from database when deleting plugin

The question:

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.

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

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.

Method 2

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");
}   

Method 3

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/

Method 4

If you are using “WORDPRESS PLUGIN BOILERPLATE GENERATOR” wppb

go to includesclass-…-deactivator.php

and write the following code ( modify please as your needs)

global $wpdb;

    $tableArray = [
        $wpdb->prefix . "table1",
        $wpdb->prefix . "table2",
    ];
    foreach($tableArray as $table){
        $wpdb->query("DROP TABLE IF EXISTS $table");
    }

Thanks

Method 5

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
*/}

Method 6

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.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment