Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
    
there is no way unless plugin stores that data with some (unique) prefix. e.g. –  amit Aug 4 '12 at 0:00
    
How do I do it if the plugin stores data with a prefix –  Matthew Hui Aug 4 '12 at 0:29

4 Answers 4

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_%`
        "
        )
);
share|improve this answer

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.

share|improve this answer

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

share|improve this answer
  • 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
}
share|improve this answer
    
"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." I think you mean at the time of deactivation. Uninstallation is at the time of delete. –  J.D. Jan 4 at 14:39
    
Yes, I meant at the time of deactivation. –  Manish Jan 5 at 19:37

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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