Friday, February 19, 2010

Is your msdb database growing huge? Do you need to clean up a lot of job history as well as database mail log?

MSDB database has a log of all the job history and also the database mail that can keep on growing and this is one thing a DBA team should be avoiding.

Below is a quick script that erases everything older than 90 days and I schedule it to run every week. This deletes all the logs older than 90 days, which is more than enough of log information unless one has a specific reason to have more information logged all the time.

 DECLARE @deletebeforedate datetime
SET @deletebeforedate = DATEADD(dd, -90, GETDATE())
EXEC msdb.dbo.sp_delete_backuphistory @deletebeforedate
EXEC msdb.dbo.sp_purge_jobhistory  @oldest_date = @deletebeforedate
EXEC msdb..sp_maintplan_delete_log null,null,@deletebeforedate
EXEC msdb.dbo.sysmail_delete_mailitems_sp @sent_before = @deletebeforedate
EXEC msdb.dbo.sysmail_delete_log_sp @logged_before = @deletebeforedate

  


No comments:

Post a Comment

Display the list of indexes and columns for a given table

You might find yourself in a situation where you have to analyze the indexes for a given table and want to know which columns are part of wh...