JSON.DEL

Introduction and Use Case(s)

JSON.DEL is a Redis command provided by the RedisJSON module to delete a key or a specific path in a JSON document. It is useful for managing parts of complex JSON objects stored in Redis, allowing you to remove unnecessary or outdated information without affecting other data.

Syntax

  1. JSON.DEL <key> [path]

Parameter Explanations

  • key: The key where the JSON document is stored.
  • path: (Optional) A JSONPath expression that specifies the part of the JSON document to delete. If omitted, the entire JSON document will be deleted.

Return Values

  • (integer): The number of paths deleted (0 if the key does not exist or the path does not match).

Code Examples

  1. dragonfly> JSON.SET mydoc $ '{"name": "John", "age": 30, "city": "New York"}'
  2. OK
  3. dragonfly> JSON.DEL mydoc $..age
  4. (integer) 1
  5. dragonfly> JSON.GET mydoc
  6. "{\"name\":\"John\",\"city\":\"New York\"}"
  7. dragonfly> JSON.SET mydoc $ '{"name": "John", "age": 30, "city": "New York"}'
  8. OK
  9. dragonfly> JSON.DEL mydoc
  10. (integer) 1
  11. dragonfly> JSON.GET mydoc
  12. (nil)

Best Practices

  • Always ensure the path exists before attempting to delete it to avoid unexpected behavior.
  • Use JSON.TYPE to verify the structure of the JSON document if uncertain about the path.

Common Mistakes

  • Providing an incorrect path which results in a zero deletion count.
  • Assuming the entire document is deleted when specifying a path; only the specified part is removed.

FAQs

Can I use wildcards in the path for JSON.DEL?

Yes, you can use JSONPath expressions with wildcards to target multiple elements for deletion.

What happens if the key does not exist?

The command returns (integer) 0, indicating no paths were deleted.