Several of my ASP.NET web applications on Cloud create large numbers of log files on a fairly regular basis and it is necessary for me to prune those logs from time to time. Because ASP.NET in medium trust can’t talk to the /logs folder which automatically purges itself every 6 days, I built a Python script which will automatically delete all files older than a specified time interval within a given directory.
If you need this functionality just save the script, update the path accordingly and set up a Chron job to call it. The code below is set to delete files older than 7 days (7 * 86400), customize as needed. Enjoy!
# Purges all ELMAH log files older than 7 days and all tempfiles older than 3 days. import os, time def purgeDir(dir, age): print "Scanning:", dir for f in os.listdir(dir): now = time.time() filepath = os.path.join(dir, f) modified = os.stat(filepath).st_mtime if modified < now - age: if os.path.isfile(filepath): os.remove(filepath) print 'Deleted: %s (%s)' % (f, modified) # 1 Day = 86400 seconds purgeDir("/system/path/to/folder", (7 * 86400))