Published Oct 7, 2023
Contribute to Docs

In Luau, table.clear() is a function that removes all the elements from a table, leaving it empty. It is equivalent to iterating over the table and setting each key to nil.



The clear() function makes the specified table t empty, meaning the table does not contain any elements anymore.


Consider the following table:

local fruitTable{"🍎","🍓","🍉"}
print("Elements:" #fruitTable)

The code will return the following output:

Elements: 0

First a table fruitTable is initiated. Then the clear() function is used to remove all elements of the table. At this point, printing the number of elements will simply return 0 as there are no elements in the table.

All contributors

Looking to contribute?

Learn Luau on Codecademy