Advertisement
Intermediate Redis, Hashes in Redis

How do you check if a specific field exists within a hash, and how do you delete just one field without affecting the rest of the hash?

Short answer

You use HEXISTS to check whether a specific field is present in a hash, returning a simple true or false style result, and HDEL to remove one or more specific fields from a hash, leaving the remaining fields completely untouched.

Save Redis mock interview
Advertisement
AModel answer
You use HEXISTS to check whether a specific field is present in a hash, returning a simple true or false style result, and HDEL to remove one or more specific fields from a hash, leaving the remaining fields completely untouched.
Example

HEXISTS user:100 phoneNumber HDEL user:100 temporaryFlag

Real-world example
A user profile system checks whether an optional phone number field exists before displaying it, and removes a temporary flag field from a user's hash once it is no longer needed, without affecting any other stored profile data.
Likely follow-ups
  • What happens if you call HDEL on the last remaining field in a hash?
  • Can you delete multiple fields at once with a single HDEL call?
Related topics