Why does user_can return false for a capability during plugin deactivation?

This was a very simple mistake in my deactivation script. The function to remove my custom role was being called before the function to remove the user.

This is the part that led to confusion: When a role is removed, it becomes unavailable, but user’s assigned to that role will still retain the role in their user data. This is why a conditional check for the user belonging to this role still returned true. So I was expecting the capabilities to remain because the check for the role returned true. While the role is present in user data, the capabilities for this role are emptied.

I’m still not sure why it seems to return empty rather than explicitly returning false, even if it is treated as false, but at the time is was executed in my code, the user did not in fact have these capabilities because the role did not exist.

I believe the primary purpose for the role remaining in user data is that a role can be removed and then added again and all users previously assigned to that role will once again resume having access to all of the role’s capabilities.