w3resource

Redis Hash: HDEL

Redis HDEL Command

Redis HDEL command is used to remove the specified fields from the hash stored at a key and ignored the specified keys that do not exist within this hash. It is treated as an empty hash if the key does not exist, and this command returns 0.

Here are some common uses and scenarios for the "HDEL" command:

  • Field Removal: Remove specific fields from a hash to clean up or manage data.
  • Data Management: Delete obsolete or unwanted fields from a hash.
  • Memory Optimization:Free up memory by removing unnecessary fields.
  • Dynamic Data Updates: Modify hashes by deleting fields as needed.
  • Configuration Management: Update configurations by removing specific settings stored in a hash.

Syntax:

HDEL KEY_NAME FIELD1.. FIELDN 

Available since

2.0.0.

Return Value

Integer reply, the number of fields that were removed from the hash, not including specified but non-existing fields.

Return Value Type

Integer

Example: Redis HDEL

127.0.0.1:6379> HMSET langhash lang1 "PHP" lang2 "JavaScript" lang3 "Python"
OK
127.0.0.1:6379> HGET langhash lang1
"PHP"
127.0.0.1:6379> HGET langhash lang2
"JavaScript"
127.0.0.1:6379> HGET langhash lang3
"Python"
127.0.0.1:6379> HDEL langhash lang1
(integer) 1
127.0.0.1:6379> HGET langhash lang1
(nil)
127.0.0.1:6379> HGET langhash lang2
"JavaScript"
127.0.0.1:6379> HGET langhash lang3
"Python" 

Example: Redis Remove multiple fields

127.0.0.1:6379> HMSET langhash lang1 "PHP" lang2 "JavaScript" lang3 "Python"
OK
127.0.0.1:6379> HGET langhash lang1
"PHP"
127.0.0.1:6379> HGET langhash lang2
"JavaScript"
127.0.0.1:6379> HGET langhash lang3
"Python"
127.0.0.1:6379> HDEL langhash lang1 lang2 lang3
(integer) 3
127.0.0.1:6379> HGET langhash lang1
(nil)
127.0.0.1:6379> HGET langhash lang2
(nil)
127.0.0.1:6379> HGET langhash lang3
(nil)

Example: Redis HDEL another example

127.0.0.1:6379> HGETALL user
1) "email"
2) "[email protected]"
3) "lang"
4) "English"
5) "gender"
6) "Male"
127.0.0.1:6379> HDEL user email
(integer) 1
127.0.0.1:6379> HDEL user lang gender
(integer) 2

Previous: APPEND
Next: HEXISTS



Follow us on Facebook and Twitter for latest update.