HDEL
HDEL
The HDEL command in DiceDB deletes a specified field within a hash stored at a given key. If either the key or field does not exist, no action is taken, and 0 is returned.
Syntax
Parameters
Parameter | Description | Type | Required |
---|---|---|---|
key | The key of the hash from which the field(s) are to be deleted. | String | Yes |
field | One or more fields within the hash to be deleted. | String | Yes |
Return Values
Condition | Return Value |
---|---|
Field(s) deleted successfully | Integer (Number of fields deleted) |
Field does not exist | 0 |
Key does not exist | 0 |
Wrong data type | (error) WRONGTYPE Operation against a key holding the wrong kind of value |
Incorrect Argument Count | (error) ERR wrong number of arguments for 'hdel' command |
Behaviour
When the HDEL
command is executed, DiceDB performs the following steps:
- It checks if the key exists in the database.
- If the key exists and is of type hash, it then checks for the specified field(s) within the hash.
- If the specified field(s) exist, they are deleted, and DiceDB returns the count of deleted fields.
- If the key does not exist or the field(s) are not present, it returns
0
.
Errors
-
Non-hash type or wrong data type
:- Error Message:
(error) WRONGTYPE Operation against a key holding the wrong kind of value
- Occurs if
key
holds a non-hash data structure.
- Error Message:
-
Incorrect Argument Count
:- Error Message:
(error) ERR wrong number of arguments for 'hdel' command
- Occurs if the command is not provided with the correct number of arguments (i.e., fewer than two).
- Error Message:
Example Usage
Deleting a field from a hash
Deleting multiple fields from a hash
Field does not exist
Key does not exist
Key is not a hash
Attempting to delete a field in a key that is not a hash
- Behavior: The
SET
command sets the keyuser:5000
to a string value. - Error: The
HDEL
command raises aWRONGTYPE
error because user:5000 is not a hash, andHDEL
only operates on hash data structures.
Wrong Number of Arguments
- Behavior: The
HDEL
command requires at least two arguments: the key and the field name. - Error: The command fails because
HDEL
requires at least akey
and onefield
as arguments. If these are not provided, DiceDB raises an error indicating an incorrect number of arguments.
Notes
- The
HDEL
command is essential for managing hash data in DiceDB, allowing fields to be efficiently removed when no longer needed.