EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials Redis Tutorial Redis HDEL
Secondary Sidebar
Redis Tutorial
  • Redis
    • Redis Pubsub
    • Redis GUI
    • Redis Database
    • Redis Get All Keys
    • Redis TTL
    • Redis EXPIRE
    • Redis Version
    • Redis EXPIRE
    • Redis List
    • Redis CLI Commands
    • Redis HSET
    • Redis FLUSHDB
    • Redis Key
    • Redis Persistence
    • Redis FLUSHALL
    • Redis pipeline
    • Redis SADD
    • Redis ZADD
    • Redis ZSET
    • Redis Get
    • Redis Replication
    • Redis Delete All Keys
    • Redis Use Cases
    • Redis HMSET
    • Redis Transactions
    • Redis Monitor
    • Redis HGET
    • Redis SETEX
    • Redis Rails
    • RedisJSON
    • Redis Get Key
    • Redis Auth
    • Redis gem
    • Redis Key Count
    • RedisTimeSeries
    • Redis Timeout
    • Redis LPUSH
    • Redis Architecture
    • Redis API
    • Redis Message Queue
    • Redis RPUSH
    • Redis Protocol
    • Redis Data Types
    • Redis Namespace
    • Redis ZRANGE
    • Redis Master Slave
    • Redis Memory Usage
    • Redis WATCH
    • Redis URL
    • Redis Key Value
    • Redis INCR
    • Redis EXISTS
    • Redis INFO
    • Redis HMGET
    • Redis Operator
    • Redis NOAUTH Authentication Required
    • Redis LRANGE
    • Redis RDB
    • Redis TLS
    • Redis HDEL
    • Redis WebSocket
    • Redis HGETALL
    • Redis GETSET
    • Node-RED WebSocket

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.

Redis HDEL

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.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

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:

Redis HDEL Field name

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:

Redis HDEL Specified keys

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:

Multiple Fields

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:

Redis HDEL Database

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:

Redis HDEL - Integer in 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:

defining the field value

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:

Redis HDEL - Deleting multiple fields

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:

creating the key name as stud

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:

Multiple field in single command

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
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more