Deletes a value at the specified path in a JSON document. If no path is specified, deletes the entire key.
Usage
const deletedCount = await redis.json.del("user:123", "$.age");
Arguments
The key containing the JSON document
JSONPath expression specifying which parts to delete. If omitted, deletes the entire key.
Response
The number of paths deleted. Returns 0 if the key or path doesn’t exist.
Examples
Delete entire JSON document
const deletedCount = await redis.json.del("user:123");
console.log(deletedCount); // 1
// Key is now deleted
const user = await redis.json.get("user:123");
console.log(user); // null
Delete specific field
// Before: { "name": "Alice", "age": 30, "email": "alice@example.com" }
await redis.json.del("user:123", "$.age");
// After: { "name": "Alice", "email": "alice@example.com" }
const user = await redis.json.get("user:123");
console.log(user);
// { name: "Alice", email: "alice@example.com" }
Delete nested field
// Delete a nested field
await redis.json.del("user:123", "$.address.zipcode");
Delete array element
// Before: { "tags": ["developer", "admin", "user"] }
await redis.json.del("user:123", "$.tags[1]");
// After: { "tags": ["developer", "user"] }
Delete multiple paths
// Using a wildcard path to delete multiple matching elements
const deletedCount = await redis.json.del("store:1", "$.products[*].discount");
console.log(`Deleted ${deletedCount} discount fields`);
Check if deletion was successful
const deletedCount = await redis.json.del("user:123", "$.premium");
if (deletedCount > 0) {
console.log("Premium flag removed");
} else {
console.log("Premium flag didn't exist");
}
JSONPath Syntax
$ - Root element (deletes entire document)
$.field - Delete a specific field
$.field[0] - Delete array element at index
$.nested.field - Delete nested field
$..field - Delete all matching fields recursively
$.array[*] - Delete all array elements
See Also