Is there some kind of MySQL query cache turned on by default or something? I am trying to work on a user import script and I am seeing some strange results from the username_existsfunction after I delete records from the wp_users table. After I delete the records when I call username_exists Wordpress is still returning the ID for the deleted user instead of false. Odd. What am I missing?

  • Why not go simple and $username_exists = false === username_exists( $username ) ? false : ! empty( get_userdata( (int) username_exists( $username ) ) );? if a user is deleted their associated data should be empty.. – Samuel Elh May 29 '16 at 0:29
  • Even better $username_exists = ! empty( get_user_by( 'slug', $username ) );.. – Samuel Elh May 29 '16 at 0:33

By default, WordPress uses $wp_object_cache which is an instance of WP_Object_Cache to save on trips to the database.

Trace back username_exists() function, you will notice that get_user_by() uses WP_User::get_data_by() which return user data from $wp_object_cache immediately without checking if that user exists in database. Note that $wp_object_cache is stored in memory.

It means that if you deleted an user record directly in your database without cleaning up user cache, user data is still available. That's the problem.

So what you're missing?

  1. Missing to delete user the right way.

  2. Missing to clean user cache.

| improve this answer | |
  • Thank you. My issue is I've implemented external object caching using Redis and Eric Mann's Redis Object Cache. I tried using a clean_user_cache() method call, but that did not work for clearing the Redis Object Cache. I can do a redis-cli flushall on the command line to flush the Redis cache after running the SQL restore and that works. Now, if I can, I'd like to check and clear the object cache for Redis from inside my Wordpress plugin. I'm having a hard time finding any documentation for that. Is it possible? – Adam Christianson May 30 '16 at 20:34
  • @AdamChristianson Redis Object Cache has the same functions as WP_Object_Cache so clean_user_cache should work normally. Have you tried wp_cache_flush()? – SarahCoding May 31 '16 at 5:23

Caching can be anywhere, starting with request caching on mysql components side (unlikely to impact in this case), and object caching on wordpress side.

You are probably doing something wrong in your code, but it is always better to use the API (or wordpress admin as a front end to it) to delete things then to try to delete them by directly accessing the DB. It is not only caching that might give you problems but also user meta and other configuration settings that you are not going to take into account.

| improve this answer | |
  • Thanks Mark. I am removing the records via the Wirdpress admin, so I should be OK there. This is a Dev environment, so I shouldn't have any caching set up, but I'll keep looking. – Adam Christianson May 30 '16 at 13:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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