delete log file

  • I would like to use http://ola.hallengren.com/ script.

    There are log output files created in the folder of MSSQL\log.

    How can I setup a job to delete the old ones, I don't see that is include in maintenance job.

    Or I missed it?

    Thanks

  • There is a job, [Output File Cleanup], that is taking care of that. It is created by http://ola.hallengren.com/scripts/MaintenanceSolution.sql.

    Ola Hallengren

    http://ola.hallengren.com

  • Thanks a lot. I will check into it.

  • One thing we recently learned about the command created in Ola's output file cleanup job is that if you are using a UNC path for your backups and/or Agent Job log files then the CmdExe script will not work as UNC paths are not supported.

    In our case and as a temporary solution, since we have a 3 node cluster using SQL 2014 Availability Groups where only the backup volumes are leveraging the same shared storage volumes for our backups, we opted for just altering the Output File Cleanup jobs to use the physical drive letters since these shared volumes are usually hosted on the same node. Of course I the event of a failover we would have to make a manual adjustment but that is a better option than not cleaning up the Agent job files at all.

  • I looked at the "Output File Cleanup" Job in Ola Hallengrens script and rewrote it in powershell.

    Turning this:

    cmd /q /c "For /F "tokens=1 delims=" %v In ('ForFiles /P "$(ESCAPE_SQUOTE(SQLLOGDIR))" /m *_*_*_*.txt /d -30 2^>^&1') do if EXIST "$(ESCAPE_SQUOTE(SQLLOGDIR))"\%v echo del "$(ESCAPE_SQUOTE(SQLLOGDIR))"\%v& del "$(ESCAPE_SQUOTE(SQLLOGDIR))"\%v"

    Into this:

    powershell.exe -command & {get-childitem -path "$(ESCAPE_SQUOTE(SQLLOGDIR))" -filter *_*_*_*.txt | Where-Object {$_.LastWriteTime -lt (Get-Date).AddDays(-30)} | remove-item -verbose}

    It should handle UNC paths and i think it is a little bit easier on the eye. Cmd is probably faster to execute and it will probably work the same pretty much anywhere, the powershell snippet above has not been tested in production so be aware of that. I just did it for the sake of learning and i did it on SQL Server 2014 SP1 Enterprise. But if anyone else finds it useful, nobody is happier than me. 🙂

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply