Whats the best way to clear a table?

Lets say I have a table local table = {"a", "b", "c", "etc..."}. And it contained lots of data. But I want to clear the table. Would it be best to scan through the whole table and remove each item from the table one by one using table.remove()? Or should I just do table = {} -- This should make the table store nothing? Right? ?

8 Likes
for k in pairs (table) do
    table [k] = nil
end

This is what I use in my script to save blocks, there may be a better way to do it out there but this works for me.

@ArticGamerTV perfect timing :sunglasses:

39 Likes

I would do it like this:

for k in pairs (table) do
    table[k] = nil
end

@mastone1 dude lol 1 second after xD

21 Likes

This creates a new table, not clearing out the current table.

If you’re clearing out a table entirely, its pointless to shift elements. (which is what table.remove is for)

If doing table = {} changes everywhere table is used, it would be fine to change to a new table this way. (usually)

To actually clear out the table, you would need to remove each element of the table.

local function ClearTable(tbl)
    for key in pairs(tbl) do
        tbl[key] = nil
    end
end
6 Likes

Correct me if I’m wrong.

Lua only has references to values, which means that when nothing is referring to a value, it will be garbage collected automatically. So, you can simply do

table = nil
table = {}

Also, I assume you only used the name “table” as an example; otherwise you should change the name.

7 Likes

Most of the time, replacing the table with {} is fine: you aren’t clearing the table - instead you are making a fresh new table and leaving the old one waiting to be garbage collected.

But if this table is used anywhere at all outside your function, then nothing will happen to it, because you aren’t actually clearing the table. You likely need to learn that tables are objects, not simple data such as a number or string.

A good analogy is a part. If you were to create a part (part = Instance.new("Part")), then didn’t need it anymore, then just using part = Instance.new("Part") would not destroy the original part in the variable part - it’d just make the script forget the original part. It will still stick around if it is referenced by anything else, e.g. another script or if it is in the world. If multiple scripts have this part in variable part, then part.Transparency = 0.5 would change the transparency for every script, because they’re all referencing this same part. That’s what it means to be an object. Conversely, if multiple scripts had number = 30, and one script changed number to 50, then the number wouldn’t change in the other scripts, because the number is a simple data type, not an object.

24 Likes

Is “k” where the table’s name would go? Or is table where the table’s name would go, if so, what does the k mean?

Also, since this topic was made in 2019 I’m not sure if they had table.clear so I’m wondering if I can use that instead?

1 Like

There’s a function for that

local t = {a = "Fizz", b = "Buzz"}

table.clear(t)

It does exactly the same as

for k in pairs (table) do
    table[k] = nil
end

Here’s some documentation about it

https://developer.roblox.com/en-us/api-reference/lua-docs/table

12 Likes

k Is the key / index used to access an item within that table. Basically, what that block of code is doing, is set k of table equal to nil, and it’s doing it to every item in the table (because of the for loop).

1 Like

for anyone new seeing this post and wondering, I believe table.clear(table) is the best way currently.

table.clear(table) – sets every index in the current table to nil

2 Likes

Since I’ve started using tables. I’ve never found out that table.clear() existed.

I’ve been doing this method

for i,_ in pairs(table) do
		table.remove(table,i)
	end

Using table.clear(table) More Characters

According to the documentation, Roblox still keeps references to it, and it advises users to only use table.clear if the user was going to re-use the table, not delete it.

1 Like

That’s kinda the point. If you’re gonna clear a table, you’re pretty much certan that you’re gonna re-use it again.

What if I want to have that old table gc-ed?

Say:

local a = {
	t = Instance.new("Part")
}
a.t:Destroy()
a = {}

Would a be gc-ed? What if the part isn’t Destroyed?

Thanks, saved the day. Setting to nil was destroying the table object so couldn’t add back to it.

table.clear(a) would have it garbage collected. As long as there are no other references to a.t, it would be garbage collected.

Other references would include something like this:

local a = {
	t = Instance.new("Part")
}

b = {
    reference = a.t
}

table.clear(a) --> wouldn't be collected unless you removed b.reference

In my testing, table.clear() doesn’t seem to work as intended in clearing out the memory. Here’s my post explaining it in detail How does Garbage Collection on Tables work?

Help would be greatly appreciated!