The most efficient way to delete millions of files based on modified date, in windows
<p>Goal: Use a script to run through 5 million - 10 million XML files and evaluate their date, if older than 90 days delete the file. The script would be run daily.</p>

<p>Problem: Using powershell Get-ChildItem -recurse, causes the script to lock up and fail to delete any files, I assume this is because of the way Get-ChildItem needs to build the whole array before taking any action on any file.</p>

<p>Solution ?: After lots of research I found that [System.IO.Directory]::EnumerateFiles will be able to take action on items in the array before the array is completely built so that should make things more efficient (<a href="https://msdn.microsoft.com/library/dd383458%28v=vs.100%29.aspx" rel="noreferrer">https://msdn.microsoft.com/library/dd383458%28v=vs.100%29.aspx</a>). After more testing I found that <code>foreach ($1 in $2)</code> is more efficient than <code>$1 | % {}</code>
Before I run this new code and potentially crash this server again is there any adjustment anyone can suggest for a more efficient way to script this?</p>

<p>For testing I just created 15,000 x 0.02KB txt files in 15,000 directories with random data in them and ran the below code, I used 90 seconds instead of 90 days on the <code>$date</code> variable just for the test, it took 6 seconds to delete all the txt files.</p>

<pre><code>$getfiles = [System.IO.Directory]::EnumerateFiles("C:\temp", "*.txt", "AllDirectories")
$date = ([System.DateTime]::Now).AddSeconds(-90)
foreach ($2 in $getfiles) {
if ([System.IO.File]::GetLastWriteTime($2) -le $date) {
[System.IO.File]::Delete($2)
} #if
} #foreach
</code></pre>