Skip to main content

Usage

Removes the specified fields from the hash stored at key. Specified fields that do not exist within this hash are ignored.
await redis.hdel(key, ...fields);

Parameters

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

Response

count
0 | 1
Returns 1 if at least one field was removed, 0 if no fields were removed (either because they didn’t exist or the key doesn’t exist).

Examples

Delete a single field

import { Redis } from '@upstash/redis';

const redis = new Redis({
  url: process.env.UPSTASH_REDIS_REST_URL!,
  token: process.env.UPSTASH_REDIS_REST_TOKEN!,
});

// Set hash fields
await redis.hset('user:1000', {
  name: 'Alice',
  email: '[email protected]',
  age: 30,
  city: 'New York'
});

// Delete a single field
const deleted = await redis.hdel('user:1000', 'age');
console.log(deleted); // 1

// Verify deletion
const user = await redis.hgetall('user:1000');
console.log(user); // { name: 'Alice', email: '[email protected]', city: 'New York' }

Delete multiple fields

// Delete multiple fields at once
const deleted = await redis.hdel('user:1000', 'email', 'city');
console.log(deleted); // 1

Delete non-existent field

// Try to delete a field that doesn't exist
const deleted = await redis.hdel('user:1000', 'phone');
console.log(deleted); // 0

Delete from non-existent key

// Try to delete from a key that doesn't exist
const deleted = await redis.hdel('user:9999', 'name');
console.log(deleted); // 0

Remove sensitive data

// Store user data with temporary token
await redis.hset('user:1000', {
  name: 'Alice',
  email: '[email protected]',
  tempToken: 'xyz789'
});

// Remove the temporary token after use
await redis.hdel('user:1000', 'tempToken');

Clean up cache fields

// Remove cached computed values
await redis.hdel('product:500', 'cachedPrice', 'cachedInventory', 'cachedRating');

Notes

  • If the key does not exist, it is treated as an empty hash and the command returns 0
  • The return value indicates whether any field was removed, not the count of removed fields
  • If all fields are removed, the key itself will be automatically deleted

See Also

  • HSET - Set hash field values
  • HEXISTS - Check if a hash field exists
  • HGET - Get a hash field value

Build docs developers (and LLMs) love