WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How to code plugin to cleanup after itself if uninstalled? (9 posts)

  1. garbonzo
    Member
    Posted 6 years ago #

    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

  2. Michael Torbert
    WordPress Virtuoso
    Posted 6 years ago #

    DROP TABLE

  3. garbonzo
    Member
    Posted 6 years ago #

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

    thanks

  4. Michael Torbert
    WordPress Virtuoso
    Posted 6 years ago #

    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?

  5. flick
    Member
    Posted 6 years ago #

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

  6. Michael Torbert
    WordPress Virtuoso
    Posted 6 years ago #

    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.

  7. Samuel Wood (Otto)
    Tech Ninja
    Posted 6 years ago #

    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');
  8. flick
    Member
    Posted 6 years ago #

    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!

  9. garbonzo
    Member
    Posted 6 years ago #

    Neato, thanks everybody.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.