Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

What's a good Windows command line option for deleting all files in a given folder older than (n) days?

Also note there may be many thousands of these files, so forfiles with a shell to cmd is not a great idea here.. unless you like spawning thousands of command shells. I consider that a pretty nasty hack, so let's see if we can do better!

Ideally, something built into (or easily installable into) Windows Server 2008.

share|improve this question
A new method based in a .BAT file that use internal CMD.EXE commands only has been posted here:… – user114382 Mar 17 '12 at 3:57
up vote 38 down vote accepted

I looked around a bit more and found a powershell way:

Delete all files more than 8 days old from the specified folder (with preview)

dir |? {$_.CreationTime -lt (get-date).AddDays(-8)} | del -whatif

(remove the -whatif to make it happen)

share|improve this answer
To confirm, does this permanently delete the files or recycle them? – TimS Oct 18 '13 at 11:11
Ops too hard for memorize!!i love powershell but i think better way is using robocopy – AminM Sep 16 '14 at 8:56

Love Jeff's PowerShell command, but for an alternative vbs solution for Windows machines without PowerShell you could try the following.

Save as (filename).vbs and execute:
*(filename).vbs (target_dir) (NoDaysSinceModified) (Action)*

The third parameter, (Action) is optional. Without it the files older than (NoDaysSinceModified) will be listed. Withit set as 'D' it will delete files older than (NoDaysSinceModified)


PurgeOldFiles.vbs "c:\Log Files" 8
will list all files in c:\Log Files older than 8 days old

PurgeOldFiles.vbs "c:\Log Files" 8 D
will delete all files in c:\Log Files older than 8 days old

note: this is a modified version of Haidong Ji's script on

Option Explicit
on error resume next
    Dim oFSO
    Dim sDirectoryPath
    Dim oFolder
    Dim oFileCollection
    Dim oFile
    Dim iDaysOld
    Dim fAction

    sDirectoryPath = WScript.Arguments.Item(0)
    iDaysOld = WScript.Arguments.Item(1)
    fAction = WScript.Arguments.Item(2)
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    set oFolder = oFSO.GetFolder(sDirectoryPath)
    set oFileCollection = oFolder.Files

If UCase(fAction) = "D" Then
'Walk through each file in this folder collection. 
'If it is older than iDaysOld, then delete it.
    For each oFile in oFileCollection
    	If oFile.DateLastModified < (Date() - iDaysOld) Then
    	End If
'Displays Each file in the dir older than iDaysOld
    For each oFile in oFileCollection
    	If oFile.DateLastModified < (Date() - iDaysOld) Then
    		Wscript.Echo oFile.Name & " " & oFile.DateLastModified
    	End If
End If

'Clean up
    Set oFSO = Nothing
    Set oFolder = Nothing
    Set oFileCollection = Nothing
    Set oFile = Nothing
    Set fAction = Nothing
share|improve this answer
I use something like this to delete old web server logs. Has worked out pretty well. – jeffspost Aug 4 '09 at 13:32

Have a look at this as this is what I use.

simple but works. delold -d 14 Deletes files older that 14 days in the current folder.

share|improve this answer

Something similar can be achieved with cygwin's (or other alternative) "find" command. But this would require you to install cygwin or have the portable version at hand.

share|improve this answer

Not really command line, but I like using LINQPad as a C# scripting host:
(which just gave me an idea for a command line C# scripting thingie à la vbs files)

var files = from f in Directory.GetFiles(@"D:\temp", "*.*", SearchOption.AllDirectories)
            where File.GetLastWriteTime(f) < DateTime.Today.AddDays(-8)
    		select f;

foreach(var f in files)
share|improve this answer
Linq is really awesome, I wish the would bring in to PowerShell. – Taylor Gibb Dec 22 '12 at 17:11

Another alternative I used to use before powershell:

share|improve this answer

I use autoIT to accomplish this on my systems. I like that you can compile .au3 files to exe easily. Not as easy to introduce a security flaw as it is with a bat file anyone can edit.

share|improve this answer

protected by Chris S May 18 '12 at 14:46

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.