Right now, I have this code:

function mr_np_activate(){
    // hook uninstall
    if ( function_exists('register_uninstall_hook') )

 * Delete options

function mr_np_uninstall() {

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?


Homem Robô


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


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.

  • As per your quote from the codex, you shouldn't add just one line of code to the uninstall.php file, you also need to check if the WP_UNINSTALL_PLUGIN constant is defined. If you don't do that, then I could visit example.com/wp-content/plugins/my-plugin/uninstall.php in my browser and the option would get deleted. – J.D. Aug 12 '17 at 12:44
  • @J.D. No, doing that would throw a function not defined error, and have no effect. Checking the constant only avoids such errors being logged, and protects against the extremely unlikely event that it's erroneously included. – Walf May 22 '19 at 3:36
  • @Walf Ah, you're right. – J.D. May 22 '19 at 14:03

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' );
  • 3
    This was about registering an uninstall hook, not a deactivation hook. There is no need to remove the option on deactivation. If the user reactivates the plugin rather than uninstalling it the settings may have changed unexpectedly then. Unless you specifically want that behavior for some reason, you should only uninstall when the plugin is uninstalled, not when it is just deactivated. – J.D. Aug 12 '17 at 12:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.