Home > Windows Server 2003 > Delete files older than certain number of days

Delete files older than certain number of days

I was recently asked to investigate a problem with a server and the lack of space on a partition. After a quick look using Treesize I noticed a suspicious folder being used by an application for logging purposes.

There were over 700,000 files in it! When I tried to browse the folder using explorer it took an absolute age to open as you can imagine. To rectify the problem and recover the majority of the disk space being used by the logs I wanted to delete the contents of the logging folder that was older than 30 days. After a quick search I discovered a command line utility called forfiles.exe that is included with Windows Server 2003. Using forfiles.exe I was able to delete all files older than 30 days like so:

forfiles.exe /p (pathtofilestodelete) /s /m *.* /d -30 /c "cmd /c del /q @path"

A working example is:

forfiles.exe /p d:\logs /s /m *.* /d -30 /c "cmd /c del /q @path"

This will delete ALL files from d:\logs (and all sub folders it contains because /s has been used to force recursion) older than 30 days without prompting you to confirm deletion.

Here is an explanation of the switches I used:

/p = The path to search for the files you want to check the date of and remove
/s = Recurse subdirectories contained within the path specified using /p and check them as well
/m = The search mask to be used for the file type you want to check the date on (*.* being all files)
/d = The date to compare the files against. A standard date type can also be used (dd/mm/yyyy)
/c = The command to be used on a file that matches the /m and /d criteria
/q = Used within /c to instruct the del command to delete files quietly

Note: different operating systems came bundled with different versions of forfiles.exe. You will need to download the appropriate version of forfiles.exe for the operating system you wish to use it on.

forfiles.exe - Windows Server 2000
forfiles.exe - Windows Server 2003
forfiles.exe - Windows Server 2008

Categories: Windows Server 2003 Tags:
  1. Chris
    October 8th, 2008 at 14:09 | #1

    Thanks a lot James,

    Your example worked great for me accross a lot of Terminal Servers and saved me a lot of time.

    inverted out :)

  2. Peter Moran
    October 27th, 2008 at 12:19 | #2

    This is just what I was looking for. Thanks for all the info in a easy to digest format.

  3. gerry
    December 8th, 2008 at 14:32 | #3

    Thank you, awesome.

  4. Antony
    December 15th, 2008 at 20:12 | #4

    Thanks, perfect - just what I needed.

  5. Rick
    January 12th, 2009 at 20:37 | #5

    Just what I was looking for, thanks a lot!

  6. Andreu
    January 15th, 2009 at 09:42 | #6

    Hi, great script. But if the file has the A attribute forfile.exe doesn't delete it. Any tip?

  7. James Clements
    January 16th, 2009 at 09:44 | #7

    Hi there Andreu, that's a good question! Ok - so we know you can use forfiles.exe to recursively loop through directories and make changes to files. Instead of using it to delete files older than say 30 says you could use it to set the attributes of files older than 30 days. Here is the command to loop through a directory for all files older than 30 days and remove the 'Archive' attribute (+A) using 'attrib'.

    forfiles.exe /p (pathtofilestodelete) /s /m *.* /d -30 /c "cmd /c attrib -a @path"

  8. rc
    January 22nd, 2009 at 23:16 | #8

    for some reason i'm getting an error when running your working example.

    Error: Invalid Syntax '/c' option is not allowed more than '1' time(s)

    When i run this.

    forfiles.exe /P c:\mypath /S /M *.* /D -30 /C “cmd /c del /q @path”

  9. James Clements
    January 23rd, 2009 at 14:52 | #9

    Paste the above line into notepad. Delete each of the speech marks and replace them within notepad. You will notice they are infact a different character. Now paste the new line into a command prompt and try it.

  10. Chip
    February 20th, 2009 at 18:24 | #10

    This awesome, can you explain the @Path???? Is that to hide the path on the screen?

  11. James Clements
    February 23rd, 2009 at 10:30 | #11

    Hey Chip, @path is used by forfiles.exe to return the full path of the current file that matches the criteria (e.g. older than 30 days) that you have specified.

    So, forfiles.exe will check each and every file/folder one at a time. Then, each time it finds a match it invokes the delete command and uses the @path to give the delete command the full path of the file that needs to be deleted.

    Essentially you *almost* have a program - forfiles.exe running another program - a command prompt using the delete command. The reason we use @path is because forfiles.exe knows where the path to the file that needs to be deleted is but the command prompt running the delete command doesn't.

  12. Yakov Kagan
    March 17th, 2009 at 08:22 | #12

    When running the following (on an actually large multi-level tree with about 1000000 subfolders):

    forfiles /pc:\mypath /s /m*.tif /d-7 /c"cmd /c del /Q @PATH"

    I get an error:

    Invalid name (recursive set) line 348

    Can you help?

  13. James Clements
    March 17th, 2009 at 15:38 | #13

    Hi Yakov. The syntax is slightly wrong there. try this:

    forfiles /p c:\mypath /s /m *.tif /d -7 /c "cmd /c del /q @path"

    Does the error appear straight away? or a little while into the process?

  14. Mihai Vlad
    March 19th, 2009 at 09:16 | #14

    Hy James

    When i run your script into cmd I get this:

    'forfiles.exe' is not recognized as an internal or external command,
    operable program or batch file.

    Can you help?

  15. James Clements
    March 19th, 2009 at 09:42 | #15

    Hi Mihai, forfiles.exe is only included on Windows Server 2003/2008 and Windows Vista. If you are not using one of these operating systems then you will need to copy the file forfiles.exe from one of the above operating systems from here:

    C:\Windows\System32

  16. Mihai Vlad
    March 19th, 2009 at 14:06 | #16

    thanks a lot ;)

  17. PS
    April 10th, 2009 at 11:02 | #17

    Can u tel me how many subdirectories it will support. I want to delete 7 days older files but not able to execute above commad

    Error Message

    ERROR: Invalid syntax. '/c' option is not allowed more than '1' time(s)
    Type "FORFILES /?" for usage.

  18. James Clements
    April 11th, 2009 at 14:16 | #18

    Hey PS,

    Make sure you use the correct speech marks, paste the command into notepad and delete the speech mark characters. Then replace them in notepad and you will see they are a different character.

  19. Bilal
    April 28th, 2009 at 11:28 | #19

    Thanks it worked

  20. John
    April 28th, 2009 at 17:28 | #20

    works great, thanks

  21. Ron
    June 5th, 2009 at 21:24 | #21

    Thanks so much James. Wasn't aware of the forfile.exe utility - what a handy little thing!

  22. Peppe
    June 9th, 2009 at 10:52 | #22

    Thanks a lot!!!

  23. Ron
    June 10th, 2009 at 10:47 | #23

    Cheers! Worked first time!

  24. unixGuy
    June 15th, 2009 at 17:16 | #24

    "ERROR: UNC paths (\\machine\share) are not supported."

    haven't seen such a stupid tool lately. seems like m$ keep sticking to 'good ol dos' drive letters...

  25. James Clements
    June 15th, 2009 at 19:59 | #25

    unixGuy, I understand your frustration! Of course the simple workaround here is to map a drive on your PC. You probably knew that already :)

  26. Jasbir Singh
    June 17th, 2009 at 09:22 | #26

    Dear James
    Thanks for highlighting such a beautiful tool.But i have one issue.When im trying to run this tool it is getting executes without any error but it is not deleting any of the files.I used below sytax:

    forfiles.exe /pc:\test /s /m*.* /d-45 /c "cmd /c del /q @path"

    And also i have many files older than 45 days.

    Please Check.

    Thanks & Regards
    Jasbir Singh

  27. James Clements
    June 17th, 2009 at 11:02 | #27

    Jasbir, when pasting your syntax into notepad it became immediately clear that you had missed some spaces out between some of the command line arguments. This is what it should look like:

    forfiles.exe /p c:\test /s /m *.* /d -45 /c "cmd /c del /q @path"

  28. Wendy
    July 9th, 2009 at 14:24 | #28

    I want to delete files older than 3 years. would I use
    forfiles.exe /p c:\mypath /s /m *.* /d -01/01/2006 /c "cmd /c del /q @path"

  29. James Clements
    July 9th, 2009 at 16:29 | #29

    Hey Wendy, you are almost right. Forfiles.exe does support full dates in the format:

    +dd/MM/yyyy
    (this is for files with a modified date of greater than or equal to)

    or

    -dd/MM/yyyy
    (this is for files with a modified date of less than or equal to)

    But the quickest way for you would be to use the number of days in a year multiplied by the number of years you wish to delete. So, 365x3= 1095 days.

    So you could use this:

    forfiles.exe /p c:\mypath /s /m *.* /d -1095 /c "cmd /c del /q @path"

  30. Bibmek
    July 31st, 2009 at 09:18 | #30

    Ok. Forfiles work fine. I want logfile in another folder how many files was deleted.

  31. john
    September 24th, 2009 at 15:40 | #31

    can you use this on unc paths or mapped network drives?

  32. James Clements
    September 25th, 2009 at 14:38 | #32

    John,

    Only on a mapped drive I'm afraid. But then it only takes a second to map a drive to a UNC :)

  33. Hooner
    October 29th, 2009 at 20:54 | #33

    Hi James, this is a great tool, thanks for sharing.

    Is there a way of telling forfiles to delete any subdirectory trees it comes across within the target directory as well as the files contained within them.

    Or am I asking too much.

    Thx

  34. Khanyi
    November 2nd, 2009 at 12:58 | #34

    Gr8.....This is good...Thnx!!

  35. Richard
    December 18th, 2009 at 17:22 | #35

    Is there a way to bypass the recycle bin when deleting files using Forfiles?

  36. Richard
    December 21st, 2009 at 16:39 | #36

    With respect to my previous post, when I ran the script manually, the deleted files went to the recycle bin. Yet when it ran from within Scheduled Tasks this weekend, it deleted them permanently, bypassing the RB.

    So, it works fine and is a very handy utility. Thanks for the tips.

  37. dreamer
    December 29th, 2009 at 09:24 | #37

    nice one m8!

    saved me alot of time

    thx

  38. Praveen
    January 11th, 2010 at 12:27 | #38

    Hi,

    I tried to running this in my system(WIN-XP operating system), for some reason it is NOT deleting any files.
    I didn't had forfiles.exe in my system, I downloaded from the net and placed it in D:\forfiles.exe and tried to run forfiles command from command promt, but no success.

    Thanks

  39. Dave
    February 22nd, 2010 at 19:01 | #39

    Praveen, i'm in the same boat as you..but when i ran it on my windows 2003 machine it worked fine...i think it's something to do with the forfiles not being part of XP

  40. Ralph
    March 17th, 2010 at 18:06 | #40

    AWESOME !!! Just what I was looking for !!!!

  41. lebisol
    March 24th, 2010 at 00:16 | #41

    @Praveen
    Same here on the win2000 server....nothing gets deleted.
    Then again...it IS windows... :(
    Thanks!

  42. James Clements
    March 24th, 2010 at 14:02 | #42

    Hi lebisol. I believe the issue you are experiencing is caused by the fact you are using forfiles.exe from Windows Server 2003 on Windows Server 2000. I have updated the post to include download links for relevant versions.

  43. lebisol
    March 24th, 2010 at 17:55 | #43

    Hi James,
    Actually it turns out is the the syntax perhaps diff. version for win2000.
    I was using win2000 kit on win2000 adv. server and what did work was:
    forfiles -pC:\Folder\ -m*.* -d-7 -c"cmd /c del /q @FILE"

    Thanks for posting this, it geared me in the right direction.
    Awesome!

  44. April 6th, 2010 at 01:03 | #44

    This tool deletes the files but leaves the folders. So is there a way to delete the folders(empty) as well using this command ?
    Note that the "Modified date" of the folder changes to the command run date once the it has deleted the files.

    I managed to delete the empty folders by following the below links
    http://www.pcmag.com/article2/0,2817,803393,00.asp
    http://blogs.msdn.com/oldnewthing/archive/2008/04/17/8399914.aspx

  45. June 14th, 2010 at 21:32 | #45

    PROBLEM #38: Hi,

    I tried to running this in my system(WIN-XP operating system), for some reason it is NOT deleting any files.
    I didn't had forfiles.exe in my system, I downloaded from the net and placed it in D:\forfiles.exe and tried to run forfiles command from command promt, but no success.

    SOLUTION: for windows XP, use forfiles.exe (for 2003). This works.
    Example: forfiles.exe /P "C:\DATAHOUSED\SORT" /S /M *.vot /C "cmd /c del /q @path"

  46. cici
    July 1st, 2010 at 20:20 | #46

    I am running this on WinServ 2003, but I keep getting this error:
    ERROR: Invalid argument/option - '/q'.
    Type "FORFILES /?" for usage.

    If I take out the /q, then I'll get
    ERROR: Invalid argument/option - '@path'.

    I don't see anything wrong with my command. Any idea?

    Here's my command:
    forfiles.exe /p c:\data\log\ /s /m *.* /d -365 /c "cmd /c del /q @path"

    Thanks in advance

  47. James Clements
    July 2nd, 2010 at 12:44 | #47

    Hi cici. I have just looked and tested what the command that you used above and the syntax is correct. It actually works fine for me! I wonder if there is a problem with the '@' symbol if you have copied/pasted the command? (I have seen issues where the character set appears different when pasting into the command prompt - especially with things like quotes e.t.c)

    I assume you have tried typing it all out as well?