Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Redis HDEL

Introduction to Redis HDEL

Redis Hdel is used to remove the specified Field from the key stored in the hash. If the specified field does not exist in the hash, then it will be ignored. If the key does not exist, then it will be treated as a hash that was empty and the same command will return o in output. Redis hashes are nothing but the collection of string types and pairs of the field value data.

Key Takeaways

  • The redis hdel command returns a value indicating how many fields were deleted from the specified key.
  • HDEL command is basically used to delete the field from the specified key. We can delete single as well as multiple fields in single command from a specified key.

What is Redis HDEL?

In redis hash, the ideal data structure holds the object in multiple properties. The good thing about the redis hashes is that they will store billion of key-value pairs in the small memory which supports holding multiple objects in a small space. Basically, the redis hdel command is used to remove one or more value pairs of fields from the redis hash which was stored in the specified key.

We are removing single as well as multiple fields in a single command by using redis hdel. If we want to remove multiple fields, then we need to mention all the fields which we want to remove from the specified key. At the time of removing the field, we need to give the key name from which we are removing the field. Redis hdel command accepts the multiple arguments, we can say that time complexity is directly proportional to the number of fields that we remove.

Below is the syntax of the redis hdel command as follows. In the below syntax, we are defining the hash key.

Syntax:

HDEL hash_key_name FIELD1 FIELD2 …. FIELDN

In the above syntax, we are using three parameters to define the redis hdel. Below is the hdel command parameter description as follows:

  • HDEL – This is the redis command used to remove the field from the specified key which we have used with the hdel command.
  • Hash key name – This is the name of the key we are using with the redis hdel command. The specified key field is deleted which we have defined in the hdel command.
  • Field1 to field N – This is the key field that we are deleting from the specified key by using the hdel command. We are using single as well as multiple fields in the same command.

Redis HDEL Key

Redis hdel key is defined as the key from which we are removing the field. We are creating the key by using the hset command and deleting the field by using the hdel command. In the below example, we are creating the key name as hdel_key and defining the field name as field1, field2, and field3. Also, we are defining the field value as val1, val2, and val3 respectively.

Command:

HSET hdel_key field1 val1 field2 val2 field3 val3

Output:

After creating the key, we delete the specified field by using the hdel command. In the below example, we are deleting the field3 from hdel_key by using the hdel command as follows. After executing the hdel command on the field3 field we can see that it will be deleted from the specified key. We can check the same by using the hget command as follows.

Command:

HDEL hdel_key field3
HGET hdel_key field3

Output:

While deleting the specified field by using redis hdel it will be providing the output as integer 1. While accessing the same field by using the hget command it will be providing the output as nil. In the below example, we are deleting the multiple fields from the hdel key. We are deleting the field name as field1 and field2 as follows.

Command:

HDEL hdel_key field1 field2
HGET hdel_key field1
HGET hdel_key field2

Output:

Redis HDEL Command

As we know that redis hdel command is used to delete the field from the specified key. If the key is not present in the database then it will return the integer 0 as an output. The below example shows that we are executing the hdel command on key61, but it does not exist in the database so it will return the integer 0 in output.

Command:

HDEL key61 field1

Output:

If the field is not present which we are deleting in the key then it will return the integer 0 as an output. The below example shows that we are executing the hdel command on hdel_key, but the specified field does not exist in the hdel_key so it will return the integer 0 in output.

Command:

HDEL hdel_key field1

Output:

In the below example, we are using the hdel command to delete the single field from the key as follows. We are deleting the field name as hdel3. We are creating the key name as redis_hdel and defining the field name as hdel1, hdel2, and hdel3 also we are defining the field value as val1, val2, and val3 respectively.

Command:

HSET redis_hdel hdel1 val1 hdel2 val2 hdel3 val3
HDEL redis_hdel hdel3
HGET redis_hdel hdel3

Output:

In the below example, we are deleting multiple fields by using the hdel command. We are deleting the hdel1 and hdel2 fields from the redis_hdel key as follows.

Command:

HDEL redis_hdel hdel1 hdel2
HGET redis_hdel hdel1
HGET redis_hdel hdel2

Output:

Examples of Redis HDEL

Given below are the examples mentioned:

Example #1

In the below example, we are deleting a single field in a single command as follows. We are creating the key name as stud and defining the field names stud_no and stud_name.

Command:

HSET stud stud_no 1 stud_name ABC
HDEL stud stud_no
HDEL stud stud_name
HGET stud stud_no
HGET stud stud_name

Output:

Example #2

In the below example, we are deleting multiple fields in single command as follows. We are deleting the emp_no and emp_name fields in a single command.

Command:

HSET emp emp_no 101 emp_name ABC
HDEL emp emp_no emp_name
HGET emp emp_no
HGET emp emp_name

Output:

Conclusion

If we want to remove multiple fields, then we need to mention all the fields which we want to remove from the specified key. Redis hdel is used to remove the specified field from the key stored in the hash. If the specified field does not exist in the hash, then it will be ignored.

Recommended Articles

This is a guide to Redis HDEL. Here we discuss the introduction, redis HDEL key & command, and examples respectively. You may also have a look at the following articles to learn more –

  1. Redis Get All Keys
  2. Redis GUI
  3. Redis Pubsub
  4. Redis Version

The post Redis HDEL appeared first on EDUCBA.



This post first appeared on Best Online Training & Video Courses | EduCBA, please read the originial post: here

Subscribe to Best Online Training & Video Courses | Educba

Get updates delivered right to your inbox!

Thank you for your subscription

×