PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience
Collapse Sidebar

ClearAllChildren

This function destroys all of an Instance's children.

As Instance/Destroy also calls itself on the children of an object it is used on, this function will destroy all descendants.

Alternatives to ClearAllChildren

If the developer does not wish to destroy all descendants, they should use Instance/GetChildren or Instance/GetDescendants to loop through an object and select what to destroy. For example, the following code sample will destroy all parts in an object.

for _, instance in pairs(object:GetDescendants()) do
	if instance:IsA("BasePart") then
		instance:Destroy()
	end
end

Returns

Return Type Summary

Code Samples


Instance:ClearAllChildren

This example creates a Part and adds a few sparkle objects to the part. Then it calls Part:ClearAllChildren() to remove all of the children.

local part = Instance.new("Part")

-- add some sparkles
for i = 1, 3 do
  Instance.new("Sparkles", part)
end

print("Part has " .. #part:GetChildren() .. " children")
--> Part has 3 children

part:ClearAllChildren()

print("Part has " .. #part:GetChildren() .. " children")
--> Part has 0 children