How to Delete a Field in a Redis Hash

If you’ve got a Redis hash with a field that needs to be deleted, you can use the HDEL command.

Example

Suppose we create the following hash:

HSET user uid "Homer" gender "Male" country "US" age 35

Let’s use HKEYS to get all the fields in our hash:

HKEYS user

Result:

1) "uid"
2) "gender"
3) "country"
4) "age"

We can see that there are four fields in the hash.

Let’s use the HDEL command to delete a field:

HDEL user gender

Result:

(integer) 1

The field was deleted.

Let’s check the key for its remaining fields:

HKEYS user

Result:

1) "uid"
2) "country"
3) "age"

Only three fields remain. The other one was deleted.

Delete Multiple Fields

We can use the HDEL command to delete multiple fields. To do this, just separate them with a space:

HDEL user country age

Result:

(integer) 2