When working with GUIs specifically with ScrollingFrames, I almost always use a UIConstaint or UILayout, and sometimes I need to clear out GuiObjects and not UIComponents.
adding ClearAllChildrenOfClass / ClearAllDescendantsOfClass will significantly help with this use case where we need to target and remove specific classes from Instances
For example when we need to remove ParticleEmitter, Lights, Smoke, Sparkles, Trail, Decal, Beam, Texture from a Part / Character after a VFX is finished.
without ClearAllChildrenOfClass / ClearAllDescendantsOfClass it is difficult to remove Effects only while keeping other essential constructors like Motor6D, Welds, Attachments, etc
Specific cases for UIs:
when we need to display a list of items as pages and reuse the same Frame / ScrollingFrame
when we need to display an inventory and reuse the same Frame / ScrollingFrame
“I need X because I want to do X” is not a great example of a use case. You might want to refer to How to post a Feature Request and clarify more about the problem you’re trying to solve (e.g. what made you end up in a situation where you need to clear a bunch of children of a certain type?) + title your thread after a problem rather than a proposed solution (API signature). There may be more fitting/generic solutions one could think of if the underlying problem is more clear.
and I got this dm attaching a script from @SillyMeTimbers that provide a more general method that you can use:
function ClearAllChildrenOfClass(parent, className)
for i, ClearInstance in pairs(parent:GetChildren()) do
if className ~= nil and type(className) == "string" and ClearInstance:IsA(className) then
ClearInstance:Destroy()
end
end
end
function ClearAllDescendantsOfClass(parent, className)
for i, ClearInstance in pairs(parent:GetDescendants()) do
if className ~= nil and type(className) == "string" and ClearInstance:IsA(className) then
ClearInstance:Destroy()
end
end
end
That code is written with bad practice, it does pre-condition checks each iteration and for some reason allows the function to run at all with invalid className, and checks className but not parent. Also when iterating arrays, ipairs should be used for clarity, and you can use _ in place of unused vars.
For future readers, this is the production version of above:
function ClearAllChildrenOfClass(parent, className)
assert(typeof(parent) == "Instance", "parent is not an Instance")
assert(typeof(className) == "string", "className is not a string")
for _, child in ipairs(parent:GetChildren()) do
if child:IsA(className) then
child:Destroy()
end
end
end
function ClearAllDescendantsOfClass(parent, className)
assert(typeof(parent) == "Instance", "parent is not an Instance")
assert(typeof(className) == "string", "className is not a string")
for _, descendant in ipairs(parent:GetDescendants()) do
if descendant:IsA(className) then
descendant:Destroy()
end
end
end
Or more succinctly with Luau type annotations:
function ClearAllChildrenOfClass(parent: Instance, className: string)
for _, child in ipairs(parent:GetChildren()) do
if child:IsA(className) then
child:Destroy()
end
end
end
function ClearAllDescendantsOfClass(parent: Instance, className: string)
for _, descendant in ipairs(parent:GetDescendants()) do
if descendant:IsA(className) then
descendant:Destroy()
end
end
end