In this article, we will be discussing about cleaning up the backup and restore history logs in the MSDB system database in SQL Server.
The Necessity for Maintaining MSDB history
Sometimes, there are cases, where it is reported to me that the MSDB database is huge and needs to be shrunk again back to “normal” sizes.
One case which affects the size of MSDB, is the backup history. Every time a database backup/restore operation takes place in SQL Server, the relevant information is kept into certain system tables in the MSDB database.
SQL Server stores database backup and restoration information into the following tables that exist in the MSDB database:
- backupfile
- backupfilegroup
- restorefile
- restorefilegroup
- restorehistory
- backupset
Example of MSDB Backup History
Let’s see an example for better understanding the process.
In this example I am using a database in SQL Server 2008 called “DEMODB2008“. To this end, I performed some backup and restore operations for allowing us to check out the data stored in MSDB.
The following queries retrieve information regarding the backup and restore operations that took place on the specific database:
-- This query returns information about the backup set SELECT * FROM msdb.dbo.backupset WHERE [database_name]='DEMODB2008' -- This query returns information about the backup files including -- the logical and physical file names, drive, etc. SELECT bf.* FROM msdb.dbo.backupfile bf INNER JOIN msdb.dbo.backupset bs ON bf.[backup_set_id]=bs.[backup_set_id] AND bs.[database_name]='DEMODB2008' -- This query returns information about the filegroups that were backed up. SELECT bg.* FROM msdb.dbo.backupfilegroup bg INNER JOIN msdb.dbo.backupset bs ON bg.[backup_set_id]=bs.[backup_set_id] AND bs.[database_name]='DEMODB2008' -- This query returns information about when a database was restored. SELECT * FROM msdb.dbo.restorehistory WHERE [destination_database_name]='DEMODB2008' -- This query returns information about the physical files involved in the restoration process. SELECT rf.* FROM msdb.dbo.restorefile rf INNER JOIN msdb.dbo.restorehistory rh ON rf.[restore_history_id]=rh.[restore_history_id] AND rh.[destination_database_name]='DEMODB2008' -- This query returns information about the restored filegroups. SELECT rg.* FROM msdb.dbo.restorefilegroup rg INNER JOIN msdb.dbo.restorehistory rh ON rg.[restore_history_id]=rh.[restore_history_id] AND rh.[destination_database_name]='DEMODB2008'
The “sp_delete_backuphistory” System Stored Procedure
Instead of manually deleting these logs from the above tables, you can use a SQL Server system stored procedure instead.
This stored procedure is called “sp_delete_backuphistory” and exists in the MSDB system database.
The usage for the above stored procedure is:
sp_delete_backuphistory [ @oldest_date = ] ‘oldest_date’
Example of using “sp_delete_backuphistory”:
exec msdb.dbo.sp_delete_backuphistory '2009-01-01'; GO
The above command will clean up the backup and restore history logs up to 01/01/2009.
For more information you can visit SQL Server Books Online.
Strengthen your SQL Server Administration Skills – Enroll to our Online Course!
Check our online course on Udemy titled “Essential SQL Server Administration Tips” (special limited-time discount included in link).
Via the course, you will learn essential hands-on SQL Server Administration tips on SQL Server maintenance, security, performance, integration, error handling and more. Many live demonstrations and downloadable resources included!
Featured Online Courses:
- SQL Server 2022: What’s New – New and Enhanced Features
- Working with Python on Windows and SQL Server Databases
- Introduction to Azure Database for MySQL
- Boost SQL Server Database Performance with In-Memory OLTP
- Introduction to Azure SQL Database for Beginners
- Essential SQL Server Administration Tips
- SQL Server Fundamentals – SQL Database for Beginners
- Essential SQL Server Development Tips for SQL Developers
- Introduction to Computer Programming for Beginners
- .NET Programming for Beginners – Windows Forms with C#
- SQL Server 2019: What’s New – New and Enhanced Features
- Entity Framework: Getting Started – Complete Beginners Guide
- Data Management for Beginners – Main Principles
- A Guide on How to Start and Monetize a Successful Blog
Read Also:
- SQL Server Installation and Setup Best Practices
- The TempDB System Database in SQL Server
- SQL Server 2016: TempDB Enhancements
- tempdb growth
- Introduction to SQL Server Machine Learning Services
- Essential SQL Server Administration Tips
- …more
Check our other related SQL Server Administration articles.
Subscribe to our newsletter and stay up to date!
Check out our latest software releases!
Check out our eBooks!
Rate this article:
Reference: SQLNetHub.com (https://www.sqlnethub.com)
© SQLNetHub
Artemakis Artemiou, a distinguished Senior Database and Software Architect, brings over 20 years of expertise to the IT industry. A Certified Database, Cloud, and AI professional, he earned the Microsoft Data Platform MVP title for nine consecutive years (2009-2018). As the founder of SQLNetHub and GnoelixiAI Hub, Artemakis is dedicated to sharing his knowledge and democratizing education on various fields such as: Databases, Cloud, AI, and Software Development. His commitment to simplicity and knowledge sharing defines his impactful presence in the tech community.