Advice on deleting entire directory

joeswfcjoeswfc Member Posts: 118 ■■■□□□□□□□
Hi All,

I'm wondering if anyone could offer some advice on a script that would help me do the following:

I have a directory on a windows server 2012 r2 server named staging. Staging contains folders 1-1000 and each of these folders contain subfolders 1-1000 (1,000,000 folders in total). A lot of these subfolders contain around 100 0-2kb files.
We copied this directory a bit ago to a new SSD volume as it wasn't doing what it was supposed to very quickly.

We now want to clear up the original staging folder, but each of the 1000 top level folders take about half an hour each and the system starts performing quite slow while this is happening.

My question is, is there a script or batch file I can run which will delete the first top level folder, then say an hour later when the scheduled task runs again delete the next folder and so on?
So I want something that will look in staging and take folder 1, delete it and it's contents. Then when it next runs it will take folder 2 and do the same. Folder 3, folder 4 etc.

I thought something like:

cd c:\staging
set folder=%variable that looks for the first folder%
rmdir /s /q %folder%

Hope someone can help icon_cheers.gif

Comments

  • PJ_SneakersPJ_Sneakers Member Posts: 884 ■■■■■■□□□□
    So you just want to get rid of C:\staging?

    Does "rd /s c:\staging" not work in your situation?
  • joeswfcjoeswfc Member Posts: 118 ■■■□□□□□□□
    So you just want to get rid of C:\staging?

    Does "rd /s c:\staging" not work in your situation?

    Yes, kind of. But I want it to delete the subfolders gradually so it does not slow the system down too much for too long.

    I think I may just set up a scheduled task to delete staging, but make sure it only runs for half an hour or so at a time so it will gradually get through the folders but will keep stopping.

    Thanks
  • PJ_SneakersPJ_Sneakers Member Posts: 884 ■■■■■■□□□□
    When you say deleting it takes a half hour, are you doing it in explorer? I'm pretty sure rd /s is just going to delete the pointer to C:\staging, which shouldn't take long at all. It's like the old deltree in DOS 5.
  • wd40wd40 Member Posts: 1,017 ■■■■□□□□□□
    I think it have to delete files within the folders first, with thousands of small files it will take time.

    a bug in arcserv killed our file server by creating millions of small files, I used BartPE and opened several cmd prompts (I think more than 12) and started deleting different set of sub-folders in each cmd prompt. not really sure if this is faster than a single delete operation, it took us several hours to clean the server up.

    May be you can try that after work hours (without shutting down the server of course).
  • GSXR750K2GSXR750K2 Member Posts: 323 ■■■■□□□□□□
    You're on 2012 R2...PowerShell it. This is a super simple example that would probably need some polishing for your environment, but something along the lines of:

    $folder = C:\TopLevelBadFolder
    $folders = GCI($folder) #Gets a list of the subfolders

    if ($folders.Count -gt 0) #checks if there are any subfolders
    {
    try { Remove-Item -Path $folders[0].FullName -Force -Recurse } # deletes the first subfolder in the list and anything inside of it
    catch { } #be notified of a failure, etc.
    }
    elseif ($folders.Count -eq 0) #no more subfolders
    {
    try { Remove-Item -Path $folder -Force -Recurse } # deletes the top level folder and any files at its level
    catch { } #be notified of a failure, etc.
    }


    Manually run as desired it or task it (be advised, PowerShell and Task Scheduler don't always play nice straight out of the gate).

    -EDIT-

    My bad...coffee was still kicking in this morning. First two lines should be...

    $folder = "C:\xxxxxxxx" #whatever folder is the problem
    $folders = GCI ($folder) | Where { $_.Attributes -eq "Directory" }


    That will select only the subfolders and not every pesky little file that may be at the root of the top level folder.
  • SlowhandSlowhand Mod Posts: 5,161 Mod
    If you're looking to delete all those subdirectories and files without putting too much of a strain on the CPU and memory, you might want to think about using robocopy's mirror mode. When robocopy runs through files to copy, skip, or delete, it bypasses Indexing, which is usually the process that eats up your processor and memory resources.

    The basic idea is that you'd create an empty directory and then mirror it into C:\Staging. For example, if you created an empty directory called C:\Empty, like this:
    robocopy C:\Empty C:\Staging /MIR /W:1 /R:3 /XJ
    

    The /MIR tag mirrors the empty directory, marking all files and folders inside C:\Staging for immediate deletion, /W:1 tells robocopy to wait one second before retrying if it hits a file in use, /R:3 tells it to retry three times if a file is locked or in use before moving on to the next one, and /XJ excludes junction points, preventing nasty loops if there are pointers to other directories. (This is also useful if you end up needing to delete a directory structure that's too many levels deep for Windows to handle.)

    It's worth a shot, if you're finding that other methods aren't working.

    Free Microsoft Training: Microsoft Learn
    Free PowerShell Resources: Top PowerShell Blogs
    Free DevOps/Azure Resources: Visual Studio Dev Essentials

    Let it never be said that I didn't do the very least I could do.
  • joeswfcjoeswfc Member Posts: 118 ■■■□□□□□□□
    I think we might just ignore it for now, we have plenty of space free on the volume so its not really an issue.

    We are building a DR environment which may end up becoming live, and then we would be rebuilding our current live anyway.
Sign In or Register to comment.