Is it more more memory efficient to loop through a folder and destroy items or destroy and replace the entire folder?

This question is targeted more towards the people that understand memory in Roblox studio. My goal is the refresh a folder almost, I am wondering if it is more efficient to loop through the folder and destroy unwanted items or to just destroy and replace the folder entirely. Any information on this topic would be greatly appreciated.

--Folder created and put into workspace and replicated storage

local OurFolder = Instance.new("Folder")
OurFolder.Name = "WantToRefresh"
OurFolder.Parent = game.Workspace


for i = 1, 4 do
	local PartWeWantToKeep = Instance.new("Part")
	PartWeWantToKeep.Parent = OurFolder
	
	for i = 1, 4 do
		local ChildWeWantToRemoveWhenRefreshingFolder = Instance.new("BoolValue")
		ChildWeWantToRemoveWhenRefreshingFolder.Parent = PartWeWantToKeep
	end
end

local RepFolder = OurFolder:Clone()
RepFolder.Parent = game.ReplicatedStorage

--Here is the first way I would refresh the values

for i, v in pairs(OurFolder:GetChildren()) do
    local ChildrenNotWanted = v:GetChildren()
    for I , v in pairs(ChildrenNotWanted) do
        v:Destroy()
    end
end

--Or we can refresh this way

game.Workspace.WantToRefresh:Destroy()
local RefreshedFolder = game.ReplicatedStorage.WantToRefresh:Clone()
RefreshedFolder.Parent = game.Workspace



1 Like

https://developer.roblox.com/en-us/api-reference/function/Instance/ClearAllChildren
that is an option btw

1 Like