Tell me more ×
WordPress Answers 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

2 Answers

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

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

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.