How can I delete options with register_uninstall_hook?

Right now, I have this code:

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');


/**
 * Delete options
 *
 **/    


function mr_np_uninstall() {
    delete_option('my_plugins_options'); 
}

But when I remove my plugin, all my options are there. (I made another plugin just to show my options).

How can I delete options when plugin is removed?

Solutions

Inside the main plugin file:

// plugin activation
register_activation_hook( __FILE__, 'my_fn_activate' );
function my_fn_activate() {
    add_option( 'my_plugin_option', 'some-value' );
}

// plugin deactivation
register_deactivation_hook( __FILE__, 'my_fn_deactivate' );
function my_fn_deactivate() {
    // some code for deactivation...
}

// plugin uninstallation
register_uninstall_hook( __FILE__, 'my_fn_uninstall' );
function my_fn_uninstall() {
    delete_option( 'my_plugin_option' );
}

You could always use an uninstall.php file for the plugin instead.

http://codex.wordpress.org/Function_Reference/register_uninstall_hook

If the plugin can not be written without running code within the plugin, then the plugin should create a file named 'uninstall.php' in the base plugin folder . This file will be called, if it exists, during the uninstall process bypassing the uninstall hook.

When using 'uninstall.php' the plugin should always check for the WP_UNINSTALL_PLUGIN constant, before executing. The WP_UNINSTALL_PLUGIN constant is defined by WordPress at runtime during a plugin uninstall, it will not be present if 'uninstall.php' is requested directly.

That file would only need literally one line of code if the only intention is to remove an option.

<?php delete_option( 'your-option' ); ?>

Not actually addressing your question, just offering an alternative approach for dealing with plugin deactivation.

As to the problem, i think the issue is you're trying to add the deactivation callback during the activation hook, which just seems a little backward or incorrect to me, i'd assume deactivation hooks should be registered in the same way as the activation hook, but neither nested inside the other.

Similar questions

Delete Post Link to delete post, its meta and attachments
I used to add a delete post link in my custom theme, but for some reason it is not deleting all the attached info. Only the post is deleted. I checked the Media Library and the attachments were still there. How can I implement to delete everything linked to the post? Note that I am implementing this on the front end and not the admin side. Ok addit...
How to delete all post and attachments of a user when I delete it?
When I delete a user, WordPress can just delete the post or page of this user, not his custom post and his attachments. An idea for a special hook?
Which delete query is better with prepare or delete
Just wondering which of the two delete queries would be the preffered method or
Delete Current Author Frontend user while delete custom post type
I created custom post type using Toolset. I want to delete current post author ( front-end user ) when i delete the post. I tried below code : But it is not working to me can anyone help me here ?
Delete taxonomy and delete all post related it
I want to delete all post related on taxonomy. for e.g whenever i delete a taxonomy from back-end all posts of that taxonomy should be deleted. i tried below code with hook name is delete_term_taxonomy it's not remove posts of taxonomy, anyone have idea regarding this.
Delete postmeta when uninstall/delete plugin
I have a custom plugin I created that adds some custom meta input fields to every post I want to remove all this plugin generated rows of postmeta for each post when I uninstall/delete the plugin what code should I add to the plugin php file to do this?

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy .