Delete tables from database when deleting plugin

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.

Topic deactivated-plugin database customization plugins Wordpress

Category Web


If you are using "WORDPRESS PLUGIN BOILERPLATE GENERATOR" wppb

go to includes\class-...-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


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/


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

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

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.


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.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.