How to Clear Backup History and Job History in SQL Server?
Hello everyone. In this article, I will try to give information about how to clear backup history and job history in SQL Server.
In SQL Server, in some cases, you may want to clear the backup history and job history.
SQL Server keeps track of every backup and restore it performs. This information is recorded in the tables below.
msdb.dbo.backupfile
msdb.dbo.backupfilegroup
msdb.dbo.backupmediafamily
msdb.dbo.backupmediaset
msdb.dbo.backupset
msdb.dbo.restorefile
msdb.dbo.restorefilegroup
msdb.dbo.restorehistory
There is no automatic cleanup of backup and restore records in SQL Server, but cleaning these records can become important over time.
Before upgrading SQL Server, if any of these tables exceed 10,000 rows this may cause the upgrade to hang, so it is recommended to delete old records.
You can run the procedure called sp_delete_backuphistory to reduce the rows in all these tables. This procedure deletes all backup and restore history information for backup sets older than a specified date. An example is shown below.
EXEC msdb..sp_delete_backuphistory '20231201 12:00:00.000'
Similarly, you can revise the example below for yourself to remove records from msdb.dbo.sysjobhistory.
EXEC msdb..sp_purge_jobhistory @oldest_date = '20231201 12:00:00.000'
You can set up a Job to clear these tables, but you’ll have to put some thought into how often you need to clear them.
Good luck to everyone in business and life.