Partial record updates
Remove obsolete or user-edited attributes from a hash without replacing the entire object.
Redis · Command
HDEL deletes one or more fields from a Redis hash. It ignores missing fields, removes the hash when no fields remain, and returns the number of fields actually deleted.
HDEL removes one or more named fields from the hash stored at a key. Missing fields do not affect the result, and Redis removes the key automatically when the last remaining field is deleted.
HDEL key field [field ...]| Name | Type | Description |
|---|---|---|
key | string | The name of the key that holds the hash. |
field [field ...] | string | One or more fields to delete from the hash. |
An integer: the number of fields removed, excluding requested fields that did not exist.
HSET user:42 name "Ada" role "admin"
HDEL user:42 role name
HGETALL user:42Creates two hash fields, removes both, then shows that the hash is empty and has been deleted.
Remove obsolete or user-edited attributes from a hash without replacing the entire object.
Delete several known hash fields in one request.
Clean up a hash and let Redis remove its key when the last field disappears.
HDEL removes the requested fields from a hash and returns the number of fields actually removed. Requested fields that were absent are excluded from that count.
Follow-up: What happens when the final field is removed? Redis deletes the hash key, so subsequent access treats it as nonexistent.
Its syntax is HDEL key field [field ...]. The command accepts one hash key and one or more fields in a single operation.
Follow-up: How would you remove fields from several unrelated hashes? Issue a separate HDEL command for each hash key; HDEL operates on one key per invocation.
HDEL has linear complexity in the number of fields supplied for deletion, shown as O(N) in the command example. It is classified as a write and hash command.
Follow-up: What determines N here? N is the number of fields passed to the command, not the total number of fields already stored in the hash.
HSETHGETHGETALLHEXISTSHGETDELDEL