How to Drop Tables on Uninstall using WordPress?

On plugin activation, I have 4 tables that are created in the WordPress database.

On uninstall I’d like to have the tables deleted.

Read More

I’ve been able to write it out to delete the tables on deactivation. But from what I’ve read, it’s better to keep the database tables information until the admin uninstalls rather than deactivates.

I’ve been looking for answers but all I seem to able to find is dropping them on deactivation. I do also have the version option I need to uninstall with it.

Related posts

Leave a Reply

6 comments

  1. You should not make uninstall.php as bhuthecoder said.
    Using an uninstall.php file to handle plugin uninstallation can be problematic because it relies on the plugin being properly installed and activated before it can be run. If the plugin was never activated, or if it was deactivated and then uninstalled, the uninstall.php file will not be run and any cleanup tasks that it was responsible for will not be performed. This can lead to plugin-specific data being left behind in the database, which can cause problems if the plugin is re-installed at a later time. A better approach is to use a WordPress uninstall hook.

    You can name the file like you want or you can do that in single file without separate file for uninstallation.

    register_uninstall_hook('uninstall.php', 'on_uninstall');
    

    It means that WP will run uninstall.php and function on_uninstall in the uninstall.php when the plugin will be deleted. So, you can rename the file if you want, you can rename the function.

    register_uninstall_hook(__FILE__, 'on_uninstall');
    

    It means the same, but __FILE__ indicate current file which is working now and function on_uninstall should be in this file.

    The full path and filename of the file with symlinks resolved. If used
    inside an include, the name of the included file is returned.

    Simple example of functions for activation/deactivation/uninstallation.

    function on_activation()
    {
        //Some stuff
    }
    
    function on_deactivation()
    {
        //Some stuff
    }
    
    function on_uninstall()
    {
        //Some stuff
    }
    
    register_activation_hook(__FILE__, 'on_activation');
    register_deactivation_hook(__FILE__, 'on_deactivation');
    register_uninstall_hook(__FILE__, 'on_uninstall');
    

    If you added some options you can delete it when uninstall like this:

    delete_option( 'some name' );
    

    As bhuthecoder said, you should not drop tables when deactivation, it is redundant and not friendly for a user, who can lost his data after deactivation.

    There are syntax mistake in your code:

    $sql = 'DROP TABLE IF EXISTS $table_name;';
    

    Should be like this:

    $sql = "DROP TABLE IF EXISTS $table_name";
    

    If you want to put some variable in the string you should use double quotes.

    And require_once is redundant.
    Better like this:

    function e34s_db_clients_uninstall()
    {
        global $wpdb;
        $table_name = $wpdb->prefix . 'e34s_clients';
        $sql = "DROP TABLE IF EXISTS $table_name";
        $wpdb->query($sql);
        delete_option('e34s_time_card_version');
    }
    
    register_uninstall_hook(__FILE__, 'e34s_db_clients_uninstall');
    

    Or like this:

    register_uninstall_hook('uninstall.php', 'e34s_db_clients_uninstall');
    

    With function e34s_db_clients_uninstall in the uninstall.php.

  2. there are two ways to do this operation on plugin uninstall/delete.

    the best way i learned is by using uninstall.php

    uninstall.php only loads when a plugin is removed or deleted from the plugins you can put any code in it to run when user deletes or uninstalls a plugin.

    <?php
    /* 
     * Removing Plugin data using uninstall.php
     * the below function clears the database table on uninstall
     * only loads this file when uninstalling a plugin.
     */
    
    /* 
     * exit uninstall if not called by WP
     */
    if ( !defined( 'WP_UNINSTALL_PLUGIN' ) ) {
        exit();
    }
    
    /* 
     * Making WPDB as global
     * to access database information.
     */
    global $wpdb;
    
    /* 
     * @var $table_name 
     * name of table to be dropped
     * prefixed with $wpdb->prefix from the database
     */
    $table_name = $wpdb->prefix . 'table_name_to_be_dropped';
    
    // drop the table from the database.
    $wpdb->query( "DROP TABLE IF EXISTS $table_name" );
    

    change the “table_name_to_be_dropped” by your table name.

  3. Well, I got many results when I googled this issue. Here is what I got:-

    function mr_np_activate(){
        // hook uninstall
        if ( function_exists('register_uninstall_hook') )
            register_uninstall_hook(__FILE__,'mr_np_uninstall');        
    }
    register_activation_hook(__FILE__,'mr_np_activate');    
    
    function mr_np_uninstall() {
        delete_option('my_plugins_options'); 
    }
    

    source: https://wordpress.stackexchange.com/questions/24600/how-can-i-delete-options-with-register-uninstall-hook

    For more info: http://codex.wordpress.org/Function_Reference/register_uninstall_hook

  4. Drop tables only when admin uninstall/delete the plugin.

    Don’t drop tables when admin deactivates the plugin , as Users normally deactivates all plugins to troubleshoot the wordpress errors after fixing they will reactivate the plugins again,if you drop tables at that time they will loose all your plugin settings that configured by admin .Also while upgrading wordpress automatically deactivates all plugins and reactivates after successful upgrade.

    Instructions to delete tables while unistalling:

  5. use register_deactivation_hook. thats work for me.

    function on_deactivation() {
    
        global $wpdb;
        $table_name = $wpdb->prefix . 'school';
        $sql        = "DROP TABLE IF EXISTS $table_name";
        $wpdb->query( $sql );
        delete_option( 'wp_install_uninstall_config' );
    }
    
    register_deactivation_hook( __FILE__, 'on_deactivation' );