Skip to main content
The HGETDEL command returns the values of the specified fields and then atomically deletes them from the hash. This is useful when you need to retrieve and remove data in a single atomic operation.

Arguments

key
string
required
The key of the hash.
fields
...string[]
required
One or more field names to get and delete.

Response

An object containing the field names and their values in the format {[fieldName: string]: TValue | null}. Returns null for fields that do not exist. If the hash doesn’t exist or all fields are non-existent, null is returned.
// Set some hash fields
await redis.hset("user:123", { name: "John", age: "30", email: "john@example.com" });

// Get and delete specific fields
const result = await redis.hgetdel("user:123", "name", "email");
console.log(result); // { name: "John", email: "john@example.com" }

// Verify fields were deleted
const name = await redis.hget("user:123", "name");
console.log(name); // null

Use Cases

  • Session Management: Retrieve and invalidate session data atomically
  • Cache Cleanup: Get cached data while removing it from storage
  • Queue Processing: Fetch and remove job data in a single operation
  • Temporary Data: Retrieve one-time use tokens or codes while deleting them