Delete all files below root directories

If I had a list of hundreds or thousands of root directories such as:

Z:> dir
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday

Is there a way to delete everything within those root directories and keep the root directories?

Thank you!

Joe,
Welcome to the forum. :wave:t3:

When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org 1 <---- Click :point_up_2:t4: :wink:

( !! Sometimes the preformatted text button hides behind the settings gear symbol. :wink: )

The root directory in your example would be Z:\ :wink:

What have you tried so far?

This forum is for scripting questions rather than script requests. We do not write customized and ready to use scripts or solutions on request. But we would be happy to assist.

We actually expect you to make an own attempt at the first place to get your task done or to solve your problem. If you have done so already please document here what exactly you have done and show your code. Then we probably might be able to help you step further.

… and to actually answer your question: Yes - there is a way. :wink:

Apologies, I wasn’t asking for a script, just curious if it’s possible to avoid a worm hole. Sorry.

Nothing to sorry about. We are here to help. We just expect a minimum of effort by yourself.

I am building a script now and I’ll post a chunk of it. There are 18,000 root directories (file share) and I believe I can put the directories in a text file and use the “for each” process and cite the text file. I really do not want to write an 18,000+ line script like this:

Remove-Item -Path Z:\Monday*.* -Recurse -Force
Remove-Item -Path Z:\Tuesday*.* -Recurse -Force
Remove-Item -Path Z:\Wednesday*.* -Recurse -Force
Remove-Item -Path Z:\Thursday*.* -Recurse -Force
Remove-Item -Path Z:\Friday*.* -Recurse -Force
Remove-Item -Path Z:\Saturday*.* -Recurse -Force
Remove-Item -Path Z:\Sunday*.* -Recurse -Force

So my thought process is this:

Remove-Item -Path Z:(1st item in text)*.* -Recurse -Force
Remove-Item -Path Z:(2nd item in text)*.* -Recurse -Force
…and so on…

Yes I know the above syntax is incorrect which is what I mean by the “for each” referencing the directory listing from the text file

I think I’m on the right path here. Please do not reply with a solution, I am trying to learn this. Just let me know if I’m heading in the wrong direction and thank you in advance!

Yes, you are. :+1:t3:

Hmmm … that’s hard … :wink: :smirk: :love_you_gesture:t3:

Instead of using 18.000 lines of code using the base directories you could use a loop instead. Just use a Get-ChildItem on the root level and pipe the result to a Foreach-Object with the Remove-Item command.

BTW: you don’t have to re-invent the wheel again and again. In the vast majority of the cases you’re not the very first one with a given task. I’d recommend using your preferred search engine to look for code examples for your task. I’m sure you will find some promising examples in a matter if seconds. :point_up:t3: :wink: :+1:t3:

I’d also be considering alternative approaches as that’s a lot of files to delete which will take a long time and you may well have problems with locked files and files you don’t have permission to access.

One alternative would be to export a list of top-level folders, quick format the drive, then recreate the folders from the exported list.

1 Like

I actually like this idea. To give a little more background. This is a fileshare on a storage array that has a folder for every user and then some. By “Then Some” I mean some folders are designated for processes. So there are literally 18,484 folders belonging to: current users, users that may have left and came back and now they have a new user name, processes for managing a transaction. However this is supposed to be a “temp” location so I’m trying to clean it up after it’s been abused. I could recreate all the folders but I would only want to recreate current users and processes and I feel that would be another story. So I was thinking of “batching” scripts at 1000 folders at a time, then go back later and any folder with 0 data, delete it. If I recreate the folder I would have to include the process of permissions. I was hoping to not be that involved. In short, I like your idea.

I’ve been googling the foreach-object and the results are enormous. On the other side I have been playing with a manual script. The script above would only delete files within the first sub directory and nothing else. I rewrote it to look like this:

Get-ChildItem -Path Z:\Monday*.* -Recurse | Remove-Item -Force
Get-ChildItem -Path Z:\Tuesday*.* -Recurse | Remove-Item -Force
Get-ChildItem -Path Z:\Wednesday*.* -Recurse | Remove-Item -Force
Get-ChildItem -Path Z:\Thursday*.* -Recurse | Remove-Item -Force
Get-ChildItem -Path Z:\Friday*.* -Recurse | Remove-Item -Force
Get-ChildItem -Path Z:\Saturday*.* -Recurse | Remove-Item -Force
Get-ChildItem -Path Z:\Sunday*.* -Recurse | Remove-Item -Force

This gets ALL the files below the root directory but unfortunately leaves subdirectories. I think once I can delete everything below the root directories then I’ll move my focus to “foreach” I have a directory listing already in a text file so when the time comes I’ll already have that. Thanks for the help!!!

I guess you’re still overthinking this a lot.

Get-ChildItem -Path Z:\ -Directory | 
ForEach-Object {
    Remove-Item -Path "$($_.FullName)\*" -Recurse -Force
}

This will get all folders of the root (Z:). Then it will loop through all of this fodlers and delete their content recursively.

Please do not use this code right away with your productive data. Use test data until you’re satisfied with the result and you’re confident not to screw up something you cannot undo. :point_up:t3: :wink:

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.