‘username_exists’ still returns an ID even after deleting record from the database?

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.

Leave a Comment