How can I easily find and remove a lot of empty folders?

I’d appreciate any suggestions anyone can offer on this. To explain my issue further:

I’m making a very large map at the moment with a lot of roads, to do this I’ve been using a road generation plugin. The plugin isn’t the smartest in terms of optimizing for minimal part usage, so I’ve been trying to manually delete sections of the roads which are completely straight, and replace the section with a singular part.

However, the way the plugin organizes sections of road is through folders- like a lot of folders. And now that I am about halfway done with the process, I’ve been realizing I forgot to delete a lot of these folders as I went. To my knowledge, there is no easy way to sort between an empty folder and a filled folder of the same name.

So I’m wondering if there is a way to get rid of these empty folders easily, given that some of them have the same name as the ones I would like to keep (see the image below as an example). I would really not prefer to do this manually as there are like, thousands of these subfolders.

image

Would appreciate any help anyone can offer on this, thanks :slight_smile:

I’m pretty sure someone has something you can type into the Command Bar to find empty folders and delete them, but I’m not that guy…

Have you used the Search tool in the Explorer window and searched for ‘folder’?
It will at least only search folders, and you can probably Shift+click (or is it CTRL+click?) all the empty folders and click Delete just once.

Have fun :wink:

local scan = game:GetService("Workspace")

for _,obj in pairs(scan:GetChildren()) do
	if obj:IsA("Folder") then
		if #obj:GetChildren() == 0 then
			obj:Destroy()
		end
	end
end

Note that this script will only scan everything inside the SCAN value and is set to workspace
Create a backup of your place in case something goes wrong

4 Likes

i think you should be using " GetDescendants() " instead of " GetChildren() " in the scan value

2 Likes