Codectionary / Developer documentation / Luau

Luau table reuse with clear

table.clear removes every entry while preserving the table and its allocated capacity. All aliases observe the clearing because no replacement table is created. Standard Lua lacks this API.

Syntax

table.clear(cache)

Examples

Luau table reuse with clear

Run as a standalone current Luau script, or as a Script in Roblox Studio ServerScriptService. No Roblox engine APIs are required; type-annotated syntax is not standard Lua.

local cache = {last = "Ada", count = 2}
local alias = cache
table.clear(cache)
print(next(alias) == nil)
cache.count = 1
print(alias.count)
  1. Output: true, then 1. alias and cache still refer to the same table after clearing.

Best practices

  • Use clear when identity and reuse are intentional. Replacing a table instead leaves existing aliases pointing to the old object.

Runtime & versions

Luau host; Roblox engine examples require Roblox Studio. Feature availability depends on the language and runtime version used by your project; consult the official reference below.

Luau documentation

Specifications & further reading