Support » Plugins » How to code plugin to cleanup after itself if uninstalled?

  • Resolved garbonzo

    (@garbonzo)


    Can you tell me, or point me to, how to code a plugin to clean up after itself if uninstalled?

    I’m writing a little plugin for myself that adds and uses an extra table in the database, but I will not be needing the functionality at a later date. At that point I will uninstall the plugin but I don’t want to have to go into the database to remove the extra table.

    Thanks

Viewing 8 replies - 1 through 8 (of 8 total)
  • Michael Torbert

    (@hallsofmontezuma)

    WordPress Virtuoso

    DROP TABLE

    But how do I tell the plugin to ‘sense’ that it’s being uninstalled, and to therefore drop the table?

    thanks

    Michael Torbert

    (@hallsofmontezuma)

    WordPress Virtuoso

    You can try the “deactivate_” hook, but that may not work since it’s the plugin being deactivated. The easiest way is to just have a button to drop the table.
    Have you even looked at the API?

    Just wondering about the same question for any options that are entered into the options table?

    Michael Torbert

    (@hallsofmontezuma)

    WordPress Virtuoso

    DELETE FROM x-options WHERE y=z

    These are basic SQL queries. If you don’t know things like this, I would recommend not attempting to write code to mess with the database on a live site.

    Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    If you use add_option or update_option, then I would suggest looking closely at delete_option.

    To sense deactivation of the plugin, I suggest something like this:

    function myplugin_deactivation_check(){
    // oh no, I'm being deactivated! Clean up time!
    do_cleanup_stuff();
    }
    register_deactivation_hook(__FILE__, 'myplugin_deactivation_check');

    Thanks for the helpful replies, Otto42 and hallsofmontezuma. 🙂

    p/s: @halls: You’ll be glad to know that I’m not writing any plugins at all. I’ve just been reading about the advice from WLTC that plugin authors should make sure that de-activation of the plugin removes any extra bits that have been added to prevent bloating? I’m afraid I can’t remember the actual word.

    One day when I do learn to write plugins, this will be very good advice to follow!

    Neato, thanks everybody.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘How to code plugin to cleanup after itself if uninstalled?’ is closed to new replies.