DEL
The DEL
command in DiceDB is used to remove one or more keys from the database. If a given key does not exist, it is ignored. This command is fundamental for data management in DiceDB, allowing for the deletion of key-value pairs. The command returns the number of keys that were removed.
Syntax
Parameters
Parameter | Description | Type | Required |
---|---|---|---|
key | The name of the key(s) to be deleted. | String | Yes |
Return values
Condition | Return Value |
---|---|
Command is successful | Integer (number of keys successfully deleted) |
No keys match the specified pattern | 0 |
Syntax or specified constraints are invalid | error |
Behaviour
When the DEL
command is executed, DiceDB will attempt to remove the specified keys from the database. The command operates in the following manner:
- Key Existence Check: For each key specified, DiceDB checks if the key exists in the database.
- Key Removal: If a key exists, it is removed from the database along with its associated value, regardless of the value’s type.
- Count Removal: The command keeps a count of how many keys were successfully removed.
- Ignore Non-existent Keys: If a specified key does not exist, it is simply ignored and does not affect the count of removed keys.
- Return Count: The total count of removed keys is returned as the result of the command.
Errors
The DEL
command is generally robust and straightforward, but there are a few scenarios where errors might occur:
-
Wrong Type of Argument: If the command is provided with an argument that is not a valid key (e.g., a non-string type), DiceDB will raise a syntax error.
- Error Message:
(error) ERR wrong number of arguments for 'del' command
- Error Message:
-
No Arguments Provided: If no keys are provided to the
DEL
command, DiceDB will raise a syntax error.- Error Message:
(error) ERR wrong number of arguments for 'del' command
- Error Message:
Example Usage
Basic Usage
Deleting a single key foo
:
Deleting Multiple Keys
Deleting multiple keys foo
, bar
, and baz
:
In this example, if only foo
and bar
existed, the command would return 2, indicating that two keys were successfully deleted.
Deleting Non-existent Keys
Attempting to delete a non-existent key:
Complex Example
Setting multiple keys and then deleting them:
In this example:
- Three keys are set:
key1
,key2
, andkey3
. - The
DEL
command attempts to deletekey1
,key2
, andkey4
. key1
andkey2
are successfully deleted.key4
doesn’t exist, so it’s ignored.- The command returns 2, indicating two keys were deleted.
Error Example
Calling DEL
without any arguments: