On plugin activation, I have 4 tables that are created in the WordPress database.
On uninstall I’d like to have the tables deleted.
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.
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.
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.
It means the same, but
__FILE__
indicate current file which is working now and function on_uninstall should be in this file.Simple example of functions for activation/deactivation/uninstallation.
If you added some options you can delete it when uninstall like this:
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:
Should be like this:
If you want to put some variable in the string you should use double quotes.
And require_once is redundant.
Better like this:
Or like this:
With function e34s_db_clients_uninstall in the uninstall.php.
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.
change the “table_name_to_be_dropped” by your table name.
Well, I got many results when I googled this issue. Here is what I got:-
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
Use,
register_uninstall_hook
.It Registers the uninstall hook that will be called when the user clicks on the uninstall link that calls for the plugin to uninstall itself,
for details check http://codex.wordpress.org/Function_Reference/register_uninstall_hook
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:
use register_deactivation_hook. thats work for me.