# FUNCTION LOAD

> Load a Lua function library, or replace an existing one.

URL: https://docs.hotkv.com/commands/function-load/

```
FUNCTION LOAD [REPLACE] function-code
```

- Group: Scripting and functions
- Complexity: O(N) where N is the length of the code
- HotKV Cloud: all plans
- On-Premise: no license needed

`FUNCTION LOAD` compiles a library and registers its functions. The code must
start with a metadata line naming the engine and the library, such as
`#!lua name=mylib`; library names may contain letters, digits and
underscores. The code then calls `redis.register_function` once per function,
either as `redis.register_function('name', callback)` or with a table giving
`function_name`, `callback`, and optionally `flags` and `description`.

The library is saved with the dataset and replicated. Loading a library whose
name is taken fails unless you pass `REPLACE`, which swaps the old library for
the new one in one step. No function name may be registered by two libraries.

## Options

| Option | Effect |
| --- | --- |
| `REPLACE` | Replace a library of the same name instead of failing. |

## Return value

| Case | RESP2 | RESP3 |
| --- | --- | --- |
| Loaded | Bulk string: the library name | Bulk string |

Errors:

- `ERR Library '<name>' already exists` without `REPLACE`.
- `ERR Function <name> already exists` when another library registers that
  function.
- `ERR No functions registered` when the code registers no function.
- `ERR Missing library metadata`, `ERR Library name was not given`,
  `ERR Engine '<engine>' not found` and similar errors for a bad first line.
- `ERR Error compiling function: ...` when the code does not compile.
- `ERR Unknown option given: <option>` for an option other than `REPLACE`.

## Examples

```
127.0.0.1:6379> FUNCTION LOAD "#!lua name=util\nredis.register_function('echo', function(keys, args) return args[1] end)"
"util"
127.0.0.1:6379> FUNCTION LOAD "#!lua name=util\nredis.register_function('echo', function(keys, args) return args[1] end)"
(error) ERR Library 'util' already exists
127.0.0.1:6379> FUNCTION LOAD REPLACE "#!lua name=util\nredis.register_function('echo', function(keys, args) return 'v2: ' .. args[1] end)"
"util"
127.0.0.1:6379> FCALL echo 0 "hi"
"v2: hi"
```

## Compatibility

Same as Redis 7 and Valkey 8.

## See also

[FCALL](https://docs.hotkv.com/commands/fcall/index.md), [FUNCTION LIST](https://docs.hotkv.com/commands/function-list/index.md),
[FUNCTION DELETE](https://docs.hotkv.com/commands/function-delete/index.md)