WordPress.org

Ready to get started?Download WordPress

Ideas

Option to Delete Mysql data when deleting a plugin

  1. Mihail Semjonov
    Member

    mm..
    just thinking..
    what if when you'r deliting plugin there would be an alert ( just like now, when u press delete perminantly ) may be after that could come anothere alert with question:
    do you want to keep plugins data or delete it from database.
    If person selects delete than you could search throught all plugin files ( .php ) and look for add_option(), update_option() CREATE TABLE atc. key words for keeping plugins data..
    ( i hope the idea is clear ).
    and after you got all the 'keys' of add_option() you could do delete_option('keys');

    aldought idea might be good - there's a problem if plugin is using 100+ files and more.. to read each file would lead to something bad.

    as well as deliting something important ( like core 'keys' if plugin is updating )..

    Anothere way could be:
    before plugin is activated to make a copy of db and one copy after plugin is activated ( and created his options ).
    Compare these 2 fields and write in db new option keys that plugin has added..
    when uninstalling - delete these options..
    (could be count( wp_options rows ) before activated and after activated ). for ex. if plugin added 2 options the difference betwine 2 counts will be 2.. so latest added 2 rows in wp_options is from new plugin.. remember them and delete when uninstalling

    Posted: 2 months ago #
  2. Ipstenu (Mika Epstein)
    Half-Elf Support Rogue & Mod

    you could search throught all plugin files ( .php ) and look for add_option(), update_option() CREATE TABLE atc. key words for keeping plugins data..

    That's possible, but the overhead would be insane (consider Multisite, you have to check -every- site on the network, this would crash a large one).

    Also sometimes it adds settings to some places that are shared, so the removal might get tetchy.

    before plugin is activated to make a copy of db and one copy after plugin is activated ( and created his options ).

    That's a bit more unlikely :) Remember, the wp_options table has transients, and options for everything. There'll be a LOT of changes if you've used a plugin for more than an hour.

    It's a risk vs reward thing. The risk of removing the unused data is more than the reward of having it removed.

    Posted: 2 months ago #
  3. abda53
    Member

    I have a solution that I think will work great that I can build.. I will look into it and see if it possible and will let you know

    Posted: 2 months ago #
  4. Mihail Semjonov
    Member

    There'll be a LOT of changes if you've used a plugin for more than an hour.

    You missunderstood me..
    Imagine that after you press activate plugin there's an ajax action heppening wich stores count of your options rows in db table wp-options.
    Then when plugin activated, after register_activation_hook();.. you make count again and compare theme..
    if your seccond count is different then the first one you store it to db telling wich lines was added ( for exemle if before activation we had 100 lines in wp-options, but after activation 109 than it means that there been added 9 new options, we can check them not to have _transient prefix and any othere.. for ex. there left only 6, thouse 6 were added after plugin activation.. so we store these 6 keys to a new option ( with unique prefix like _wp_plugin_{plugin_name} And when we uninstall and ask do u want to delete options it makes ge_option(_wp_plugin_{plugin_name}) loopse throught all keys in there and deletes them, then delete's itself ). Do u understand now? let me know what u think.

    Anothere idea:
    Create some class, function to make deliting of options, post_types atc. easier..

    function _plugin_uninstall( $file, $args ){
    	register_uninstall_hook( $file, function() use( $args ){
    		if( isset( $args['post_types'] ) ){
    			$post_types = $args['post_types'];
    			foreach ( $post_types as $post_type ) {
    				// do post_type deliting
    			}
    		}
    		if( isset( $args['options'] ) ){
    			$options = $args['options'];
    			foreach ( $options as $option ) {
    				// do option deliting
    				delete_option( $option );
    			}
    		}
    		if( isset( $args['meta'] ) ){
    			$metas = $args['meta'];
    			foreach ( $metas as $meta ) {
    				// do meta deliting
    			}
    		}
    	} );
    }
    _plugin_uninstall( __FILE__, array(
    	'post_types' => array( 'testimonials', 'dogs', 'news' ),
    	'options' => array( 'my_plugin_options', 'othere_options' ),
    	'meta' => array( 'my_meta_keys', 'meta_keys2' )
    )  );

    and run it instead of register_uninstall_hook()

    May be these will stimulate developers to do options deleing, becouse it requires only array to pass, no need to do delete of each option manualy

    Posted: 2 months ago #
  5. Ipstenu (Mika Epstein)
    Half-Elf Support Rogue & Mod

    I didn't misunderstand you :) I just think you're overestimating the simplicity and not considering the plugins that make their own transients (galleries, eshops, etc) and ones that edit existing data (membership plugins or ones that edit user roles). Not to mention all this has to be optional :)

    I really think the idea is awesome, the theory is a great start, and I'd love to see someone bake it good and right so it covers all the angels, but it's a risk vs reward situation like I said. The risk of you accidentally deleting data I NEED is higher than the reward of having the old data deleted.

    Posted: 2 months ago #

RSS feed for this topic

Reply

You must log in to post.

  • Rating

    12345
    3 Votes
  • Status

    This idea has been implemented