Removing plugin settings from database

I am developing a little plugin that has couple options. For that i use this code:

add_action('admin_init', array($this, 'pa_register_settings'));
function pa_register_settings()
{
    register_setting('pa_options', 'pa_settings');
    register_setting('pa_options', 'pa_metrics');
    register_setting('pa_options', 'pa_token');
}

I would like to know what is the proper way to remove those settings from database when plugin is uninstalled?

Related posts

Leave a Reply

1 comment

  1. WordPress has a function called register_deactivation_hook which registers a plugin function to be run when the plugin is deactivated, so:

    register_deactivation_hook( __FILE__, 'myplugin_deactivate' );
    function myplugin_deactivate(){
       //delete plugins option here ex:
       delete_option('pa_options');
    }
    

    Update:
    Like One Trick Pony mentioned, sometimes users deactivate plugins and when they reactivate it they will loose all settings if you use this method.

    So a better one would be to create an uninstall option in your plugin’s admin panel and submitted, only then you should remove the options from the database and deactivate the plugin using deactivate_plugins( '/plugin-folder/plugin-name.php' );