• While this code does the job I have to ask, "Why would you want your SQL Server deleting files?"

    There isn't even any data-driven relationship to the files. We induct call recordings (.mp3) and store the files on disk for different duration based on the call result (sales, refusals, retry) - in that case it is the data that determines the files to delete. Rather than making thousands of cmdshell calls in a loop (or some other tedious operations) We have a [WSH] script that contacts a webservice to get a work-batch, it tracks the status of each operation in the batch and reports the results back to the database through another webservice call. There are two low-cost calls to the database and the heavy lifting on filesystem is done by a machine other than the SQL Server.

    Well, environments vary. I guess we use whatever tools we have available. Maybe when we're hammering ever-harder on that screwdriver we should stop to consider if a chisel would be the right tool for the job.