{
  "schema_version": 2,
  "id": "commands/hlen",
  "title": "HLEN",
  "url": "https://redis.io/docs/latest/commands/hlen/",
  "summary": "Returns the number of fields in a hash.",
  "since": "2.0.0",
  "tags": [
    "docs",
    "develop",
    "stack",
    "oss",
    "rs",
    "rc",
    "oss",
    "kubernetes",
    "clients"
  ],
  "last_updated": "2026-08-05T14:46:45-07:00",
  "page_type": "content",
  "content_hash": "8ebfd307f0f3f3ca74a95fbc035ec60ba316ca18a965d8a3278573a8f29218ee",
  "sections": [
    {
      "id": "required-arguments",
      "title": "Required arguments",
      "role": "content",
      "text": "<details open><summary><code>key</code></summary>\n\nThe name of the key that holds the hash.\n\n</details>"
    },
    {
      "id": "examples",
      "title": "Examples",
      "role": "example",
      "text": "Foundational: Count the fields in a hash with HLEN when you need a hash's size without transferring its contents\n\n**Difficulty:** Beginner\n\n**Commands:** HSET, HLEN\n\n**Complexity:**\n- HSET: O(1)\n- HLEN: O(1)\n\n**Available in:** Redis CLI, C, C#, Go, Java, Java (Asynchronous - Lettuce), Java (Reactive - Lettuce), Java (Synchronous - Jedis), JavaScript (Node.js), JavaScript (Node.js), PHP, Python, Ruby, Rust (Asynchronous), Rust (Synchronous)\n\n##### Redis CLI\n\n[code example]\n\n##### C\n\n[code example]\n\n##### C#\n\n[code example]\n\n##### Go\n\n[code example]\n\n##### Java\n\n[code example]\n\n##### Java (Asynchronous - Lettuce)\n\n[code example]\n\n##### Java (Reactive - Lettuce)\n\n[code example]\n\n##### Java (Synchronous - Jedis)\n\n[code example]\n\n##### JavaScript (Node.js)\n\n[code example]\n\n##### JavaScript (Node.js)\n\n[code example]\n\n##### JavaScript (Node.js)\n\n[code example]\n\n##### JavaScript (Node.js)\n\n[code example]\n\n##### PHP\n\n[code example]\n\n##### Python\n\n[code example]\n\n##### Ruby\n\n[code example]\n\n##### Rust (Asynchronous)\n\n[code example]\n\n##### Rust (Synchronous)\n\n[code example]"
    },
    {
      "id": "redis-software-and-redis-cloud-compatibility",
      "title": "Redis Software and Redis Cloud compatibility",
      "role": "content",
      "text": "| Redis<br />Software | Redis<br />Cloud | <span style=\"min-width: 9em; display: table-cell\">Notes</span> |\n|:----------------------|:-----------------|:------|\n| <span title=\"Supported\">&#x2705; Standard</span><br /><span title=\"Supported\"><nobr>&#x2705; Active-Active</nobr></span> | <span title=\"Supported\">&#x2705; Standard</span><br /><span title=\"Supported\"><nobr>&#x2705; Active-Active</nobr></span> |  |"
    },
    {
      "id": "return-information",
      "title": "Return information",
      "role": "returns",
      "text": "**RESP2:**\n\n[Integer reply](../../develop/reference/protocol-spec#integers): the number of fields in the hash, or 0 when the key does not exist.\n\n**RESP3:**\n\n[Integer reply](../../develop/reference/protocol-spec#integers): the number of the fields in the hash, or 0 when the key does not exist."
    }
  ],
  "examples": [
    {
      "id": "examples-ex0",
      "language": "plaintext",
      "code": "redis> HSET myhash field1 \"Hello\"\n(integer) 1\nredis> HSET myhash field2 \"World\"\n(integer) 1\nredis> HLEN myhash\n(integer) 2",
      "section_id": "examples"
    },
    {
      "id": "examples-ex1",
      "language": "c",
      "code": "// Add two new fields to the hash\n    reply = redisCommand(c, \"HSET %s %s %s\", \"myhash\", \"field1\", \"Hello\");\n    printf(\"HSET myhash field1 Hello: %lld\\n\", reply->integer); // >>> 1\n    freeReplyObject(reply);\n\n    reply = redisCommand(c, \"HSET %s %s %s\", \"myhash\", \"field2\", \"World\");\n    printf(\"HSET myhash field2 World: %lld\\n\", reply->integer); // >>> 1\n    freeReplyObject(reply);\n\n    // Count the fields in the hash\n    reply = redisCommand(c, \"HLEN %s\", \"myhash\");\n    printf(\"HLEN myhash: %lld\\n\", reply->integer); // >>> 2\n    freeReplyObject(reply);",
      "section_id": "examples"
    },
    {
      "id": "examples-ex2",
      "language": "csharp",
      "code": "bool hlenRes1 = db.HashSet(\"myhash\", \"field1\", \"Hello\");\n        Console.WriteLine(hlenRes1);    // >>> True\n\n        bool hlenRes2 = db.HashSet(\"myhash\", \"field2\", \"World\");\n        Console.WriteLine(hlenRes2);    // >>> True\n\n        long hlenRes3 = db.HashLength(\"myhash\");\n        Console.WriteLine(hlenRes3);    // >>> 2",
      "section_id": "examples"
    },
    {
      "id": "examples-ex3",
      "language": "go",
      "code": "hlen1, err := rdb.HSet(ctx, \"myhash\", \"field1\", \"Hello\").Result()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Println(hlen1) // >>> 1\n\n\thlen2, err := rdb.HSet(ctx, \"myhash\", \"field2\", \"World\").Result()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Println(hlen2) // >>> 1\n\n\thlen3, err := rdb.HLen(ctx, \"myhash\").Result()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Println(hlen3) // >>> 2",
      "section_id": "examples"
    },
    {
      "id": "examples-ex4",
      "language": "java",
      "code": "// `hset` returns true because `field1` is a new field.\n            boolean res19 = syncCommands.hset(\"myhash\", \"field1\", \"Hello\");\n            System.out.println(res19); // >>> true\n\n            // `hset` returns true because `field2` is also a new field.\n            boolean res20 = syncCommands.hset(\"myhash\", \"field2\", \"World\");\n            System.out.println(res20); // >>> true\n\n            Long res21 = syncCommands.hlen(\"myhash\");\n            System.out.println(res21); // >>> 2",
      "section_id": "examples"
    },
    {
      "id": "examples-ex5",
      "language": "java",
      "code": "CompletableFuture<Void> hLenExample = asyncCommands.hset(\"myhash\", \"field1\", \"Hello\").thenCompose(res1 -> {\n                // `hset` returns true because `field1` is a new field.\n                System.out.println(res1); // >>> true\n                return asyncCommands.hset(\"myhash\", \"field2\", \"World\");\n            }).thenCompose(res2 -> {\n                // `hset` returns true because `field2` is also a new field.\n                System.out.println(res2); // >>> true\n                return asyncCommands.hlen(\"myhash\");\n            }).thenAccept(res3 -> {\n                System.out.println(res3); // >>> 2\n            }).toCompletableFuture();",
      "section_id": "examples"
    },
    {
      "id": "examples-ex6",
      "language": "java",
      "code": "Mono<Boolean> hLenExample1 = reactiveCommands.hset(\"myhash\", \"field1\", \"Hello\").doOnNext(result -> {\n                System.out.println(result); // >>> true\n            });\n\n            hLenExample1.block();\n\n            Mono<Boolean> hLenExample2 = reactiveCommands.hset(\"myhash\", \"field2\", \"World\").doOnNext(result -> {\n                System.out.println(result); // >>> true\n            });\n\n            hLenExample2.block();\n\n            Mono<Long> hLenExample3 = reactiveCommands.hlen(\"myhash\").doOnNext(result -> {\n                System.out.println(result); // >>> 2\n            });",
      "section_id": "examples"
    },
    {
      "id": "examples-ex7",
      "language": "java",
      "code": "// `hset` returns 1 because `field1` is a new field.\n        long hLenResult1 = jedis.hset(\"myhash\", \"field1\", \"Hello\");\n        System.out.println(hLenResult1);    // >>> 1\n\n        // `hset` returns 1 because `field2` is also a new field.\n        long hLenResult2 = jedis.hset(\"myhash\", \"field2\", \"World\");\n        System.out.println(hLenResult2);    // >>> 1\n\n        long hLenResult3 = jedis.hlen(\"myhash\");\n        System.out.println(hLenResult3);    // >>> 2",
      "section_id": "examples"
    },
    {
      "id": "examples-ex8",
      "language": "javascript",
      "code": "const res17 = await client.hSet('myhash', 'field1', 'Hello')\nconsole.log(res17) // 1\n\nconst res18 = await client.hSet('myhash', 'field2', 'World')\nconsole.log(res18) // 1\n\nconst res19 = await client.hLen('myhash')\nconsole.log(res19) // 2",
      "section_id": "examples"
    },
    {
      "id": "examples-ex9",
      "language": "javascript",
      "code": "const hlenSet1 = await redis.hset('myhash', 'field1', 'Hello');\nconsole.log(hlenSet1); // >>> 1\n\nconst hlenSet2 = await redis.hset('myhash', 'field2', 'World');\nconsole.log(hlenSet2); // >>> 1\n\nconst hlenResult = await redis.hlen('myhash');\nconsole.log(hlenResult); // >>> 2",
      "section_id": "examples"
    },
    {
      "id": "examples-ex10",
      "language": "javascript",
      "code": "const res17 = await client.hSet('myhash', 'field1', 'Hello')\nconsole.log(res17) // 1\n\nconst res18 = await client.hSet('myhash', 'field2', 'World')\nconsole.log(res18) // 1\n\nconst res19 = await client.hLen('myhash')\nconsole.log(res19) // 2",
      "section_id": "examples"
    },
    {
      "id": "examples-ex11",
      "language": "javascript",
      "code": "const hlenSet1 = await redis.hset('myhash', 'field1', 'Hello');\nconsole.log(hlenSet1); // >>> 1\n\nconst hlenSet2 = await redis.hset('myhash', 'field2', 'World');\nconsole.log(hlenSet2); // >>> 1\n\nconst hlenResult = await redis.hlen('myhash');\nconsole.log(hlenResult); // >>> 2",
      "section_id": "examples"
    },
    {
      "id": "examples-ex12",
      "language": "php",
      "code": "$hLenResult1 = $this->redis->hset('myhash', 'field1', 'Hello');\n        echo \"HSET myhash field1 Hello: \" . $hLenResult1 . \"\\n\"; // >>> 1\n\n        $hLenResult2 = $this->redis->hset('myhash', 'field2', 'World');\n        echo \"HSET myhash field2 World: \" . $hLenResult2 . \"\\n\"; // >>> 1\n\n        $hLenResult3 = $this->redis->hlen('myhash');\n        echo \"HLEN myhash: \" . $hLenResult3 . \"\\n\"; // >>> 2",
      "section_id": "examples"
    },
    {
      "id": "examples-ex13",
      "language": "python",
      "code": "res15 = r.hset(\"myhash\", \"field1\", \"Hello\")\nprint(res15)\n# >>> 1\n\nres16 = r.hset(\"myhash\", \"field2\", \"World\")\nprint(res16)\n# >>> 1\n\nres17 = r.hlen(\"myhash\")\nprint(res17)\n# >>> 2",
      "section_id": "examples"
    },
    {
      "id": "examples-ex14",
      "language": "ruby",
      "code": "res19 = r.hset('myhash', 'field1', 'Hello')\nputs res19 # >>> 1\n\nres20 = r.hset('myhash', 'field2', 'World')\nputs res20 # >>> 1\n\nres21 = r.hlen('myhash')\nputs res21 # >>> 2",
      "section_id": "examples"
    },
    {
      "id": "examples-ex15",
      "language": "rust",
      "code": "match r.hset(\"myhash\", \"field1\", \"Hello\").await {\n            Ok(res15) => {\n                let res15: i32 = res15;\n                println!(\"{res15}\");    // >>> 1\n            },\n            Err(e) => {\n                println!(\"Error setting hash field: {e}\");\n                return;\n            }\n        }\n\n        match r.hset(\"myhash\", \"field2\", \"World\").await {\n            Ok(res16) => {\n                let res16: i32 = res16;\n                println!(\"{res16}\");    // >>> 1\n            },\n            Err(e) => {\n                println!(\"Error setting hash field: {e}\");\n                return;\n            }\n        }\n\n        match r.hlen(\"myhash\").await {\n            Ok(res17) => {\n                let res17: usize = res17;\n                println!(\"{res17}\");    // >>> 2\n            },\n            Err(e) => {\n                println!(\"Error getting hash length: {e}\");\n                return;\n            }\n        }",
      "section_id": "examples"
    },
    {
      "id": "examples-ex16",
      "language": "rust",
      "code": "match r.hset(\"myhash\", \"field1\", \"Hello\") {\n            Ok(hlen1) => {\n                let hlen1: i32 = hlen1;\n                println!(\"{hlen1}\");    // >>> 1\n            },\n            Err(e) => {\n                println!(\"Error setting hash field: {e}\");\n                return;\n            }\n        }\n\n        match r.hset(\"myhash\", \"field2\", \"World\") {\n            Ok(hlen2) => {\n                let hlen2: i32 = hlen2;\n                println!(\"{hlen2}\");    // >>> 1\n            },\n            Err(e) => {\n                println!(\"Error setting hash field: {e}\");\n                return;\n            }\n        }\n\n        match r.hlen(\"myhash\") {\n            Ok(hlen3) => {\n                let hlen3: usize = hlen3;\n                println!(\"{hlen3}\");    // >>> 2\n            },\n            Err(e) => {\n                println!(\"Error getting hash length: {e}\");\n                return;\n            }\n        }",
      "section_id": "examples"
    }
  ]
}
