Best practice way to delete user meta data during plugin uninstall?

If a plugin stores data in the usermeta tables what is the best practice method to delete these entries for all users in uninstall.php? I could access the database directly but is there another way?

Related posts

Leave a Reply

4 comments

  1. You should not access the database directly, if at all possible. There are two main reasons for this:

    • If the database structure changes (unlikely), your queries may become outdated. Using functions like delete_user_meta() will ensure that your query should work properly for all WordPress versions (past, present, and future) that support that function.

    • Caching. If you don’t clean up the cache after deleting your data, it could cause problems. If you delete your data manually, you also need to clean up the cache manually. Using built in functions that do this automatically is definitely a better idea.

    For deleting meta data (such as user meta fields), I would recommend using the delete_metadata() function. This function has a fifth parameter that you can set to true to remove the metadata with a given meta_key for all objects (in this case users). Example:

    $meta_type  = 'user';
    $user_id    = 0; // This will be ignored, since we are deleting for all users.
    $meta_key   = 'your_meta_key';
    $meta_value = ''; // Also ignored. The meta will be deleted regardless of value.
    $delete_all = true;
    
    delete_metadata( $meta_type, $user_id, $meta_key, $meta_value, $delete_all );
    

    You can repeat that for each meta key your plugin uses.

  2. It’s best not to interact with the database directly, especially when issuing DELETE statements, since a single typo can destroy unintended data. Instead, use WordPress functions to get a list of all user IDs, then remove the user meta field for each user individually, like so:

    $all_user_ids = get_users( 'fields=ID' );
    foreach ( $all_user_ids as $user_id ) {
        delete_user_meta( $user_id, 'your_meta_key_to_delete' );
    }
    

    Function reference:

    http://codex.wordpress.org/Function_Reference/get_users
    http://codex.wordpress.org/Function_Reference/delete_user_meta

  3. Best practice is to prefix the meta data your plugin enters that way you can simply do something like a search for all meta_key’s like this

    $wpdb->query( 
        $wpdb->prepare( 
            "
            DELETE FROM $wpdb->usermeta
            WHERE meta_key LIKE `_prefix_%`
            "
            )
    );
    
    • You should not access DB directly.
    • Must use WP wrapper functions to do any DB changes.
    • Never delete any plugin related data from DB at the time of plugin un installation. Because in WP there are so many cases when you have to un install your your plugins for testing purpose.
    • Always delete plugin related data from DB at the time of plugin delete. Check http://codex.wordpress.org/Function_Reference/register_deactivation_hook

    Sample code:

    register_uninstall_hook( __FILE__, 'plugin_uninstall' );
    
    function plugin_uninstall () {
        //usermeta data delete code
    }