Hash

HGETDEL

Get and delete hash fields atomically.

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

keybodystringrequired

The key of the hash.

fieldsbody...string[]required

One or more field names to get and delete.

Response

Record<string, TValue | null> | nullrequired

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.

Basic Example
// Set some hash fieldsawait redis.hset("user:123", { name: "John", age: "30", email: "john@example.com" });// Get and delete specific fieldsconst result = await redis.hgetdel("user:123", "name", "email");console.log(result); // { name: "John", email: "john@example.com" }// Verify fields were deletedconst name = await redis.hget("user:123", "name");console.log(name); // null
Multiple Fields
await redis.hset("session:abc", {   token: "xyz123",   user: "john",   expires: "2024-12-31" });// Get and delete all fieldsconst session = await redis.hgetdel("session:abc", "token", "user", "expires");console.log(session); // { token: "xyz123", user: "john", expires: "2024-12-31" }
Non-existent Fields
await redis.hset("user:456", { name: "Jane" });// Get and delete with non-existent fieldconst result = await redis.hgetdel("user:456", "name", "email");console.log(result); // { name: "Jane", email: 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
Loading search…