Forums

How to DROP TABLE from database (4 posts)

  1. MadScout
    Member
    Posted 3 years ago #

    I'm working on a plugin which creates a table on activation. How do I drop that table on deactivation? Here is my code so far, but all it does is create the table, not delete it on deactivation.

    ...other functions and things...
    function POD_deactivate()
    {
    	global $wpdb;	//required global declaration of WP variable
    
    	$table_name = $wpdb->prefix.POD_table_name;
    
    	$sql = "DROP TABLE ". $table_name;
    
    	$wpdb->query($sql);
    
    }
    register_activation_hook(__FILE__,'POD_install');
    register_deactivation_hook(__('POD') , 'POD_deactivate' );
    ?>
  2. laqrhead
    Member
    Posted 3 years ago #

    I don't know how to answer your question, but it would seem to me that you may want to leave the tables in place in case the the user decides to re-enable the plugin, the previous data is still in place. You've probably already thought of that though.

  3. NomikOS
    Member
    Posted 2 years ago #

    probably the DB user that run your blog installation do not have drop privileges.

    ----------------------

    you can view errors inside of a function:

    $sql = "DROP TABLE IF_EXISTS $table_name;";
    $e = $wpdb->query($sql);
    die(var_dump($e));
  4. mrthrust
    Member
    Posted 2 years ago #

    how about....

    function pluginUninstall() {
    
            global $wpdb;
            $table = $wpdb->prefix."your_table_name";
    
            //Delete any options thats stored also?
    	//delete_option('wp_yourplugin_version');
    
    	$wpdb->query("DROP TABLE IF EXISTS $table");
    }

    than all you do is hook into wordpress when its being de-activated

    register_deactivation_hook( __FILE__, 'pluginUninstall' );

    that should clean up the database a little..

Topic Closed

This topic has been closed to new replies.

About this Topic