SQL Server stores history of all the taken backup forever. History of all the backup is stored in msdb database. Many times older history is no more required. Following Stored Procedure can be executed with parameter which takes days of history to keep. In following example 30 is passed to keep history of month.
USE msdb
GO
DECLARE @DaysToKeepHistory DATETIME
SET @DaysToKeepHistory = CONVERT(VARCHAR(10), DATEADD(dd, -30, GETDATE()), 101)
EXEC sp_delete_backuphistory @DaysToKeepHistory
GO
Reference: Pinal Dave (http://www.SQLAuthority.com)










Also, be aware that if you don’t purge history regularly, you can end up with a really large MSDB – even just several gigs can give you performance problems when deleting history. I’ve run into servers that had to take an outage just to delete the history because it’d grown so large.
hii pinal
is it possible to restore or get back deleted backup history…………
regards
Hi.
I only wanted to mention if you’re using a German MS-SQL Server you got to replace the parameter 101 with 104 to get the correct input-Format for the sp_delete_backuphistory procedure.
i.e. SET @DaysToKeepHistory = CONVERT(VARCHAR(10), DATEADD(dd, -30, GETDATE()), 104)
regards.
How we can check the details of back history using MSDB.
Hi Pinal,
I have a doubt regarding the comments whenever a question is raised by some one hw can d solution known to d people apart from the questioner.
Thanks,
Narenndra
sir ,
i have some problem , i have 10 system and one server
and i want to delete all the search item and web history from server will be it’s possible…? if possible please send me reply
it’s n urgent………..
teenu ranga
bikaner
rajasthan
One key problem with this procedure and other similar ones is that they don’t yield to other running processes. I had to use this procedure in emergency and it can cause locking and blocking unless you have exclusive access to the tables used in the stored procedure.
This is because often many millions of rows being deleted if you are using log shipping of several databases for example that performs a frequent log backup for each one.
Maybe MS will rewrite them to give way in the next release.
Tony S.
I tweaked the System SP to make it more efficient. I ran into too many blockings and lockings with table data types. I used good old temp tables and indexed them.
Also I am deleting only the TLog backups here. You can change it to do both by removing the where clause for ‘L’
I ran it giving it 15 days at a time.
USE [msdb]
GO
/****** Object: StoredProcedure [dbo].[spDeleteBackuphistoryTLogs] Script Date: 08/26/2011 09:13:26 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO
ALTER PROCEDURE [dbo].[spDeleteBackuphistoryTLogs]
@oldest_date datetime
AS
BEGIN
SET NOCOUNT ON
SELECT DISTINCT backup_set_id INTO #TempBackupSetID
FROM msdb.dbo.backupset
WHERE type = ‘L’ and
backup_finish_date < @oldest_date
CREATE CLUSTERED INDEX CID_TBSI ON #TempBackupSetID (backup_set_id)
SELECT DISTINCT media_set_id INTO #TempMediaSetID
FROM msdb.dbo.backupset
WHERE type = 'L' and
backup_finish_date 0)
GOTO Quit
DELETE FROM msdb.dbo.backupfilegroup
WHERE backup_set_id IN (SELECT backup_set_id
FROM #TempBackupSetID)
IF (@@error > 0)
GOTO Quit
DELETE FROM msdb.dbo.restorefile
WHERE restore_history_id IN (SELECT restore_history_id
FROM #TempRestoreHistoryID)
IF (@@error > 0)
GOTO Quit
DELETE FROM msdb.dbo.restorefilegroup
WHERE restore_history_id IN (SELECT restore_history_id
FROM #TempRestoreHistoryID)
IF (@@error > 0)
GOTO Quit
DELETE FROM msdb.dbo.restorehistory
WHERE restore_history_id IN (SELECT restore_history_id
FROM #TempRestoreHistoryID)
IF (@@error > 0)
GOTO Quit
DELETE FROM msdb.dbo.backupset
WHERE backup_set_id IN (SELECT backup_set_id
FROM #TempBackupSetID)
IF (@@error > 0)
GOTO Quit
DELETE msdb.dbo.backupmediafamily
FROM msdb.dbo.backupmediafamily bmf
WHERE bmf.media_set_id IN (SELECT media_set_id
FROM #TempMediaSetID)
AND ((SELECT COUNT(*)
FROM msdb.dbo.backupset
WHERE media_set_id = bmf.media_set_id) = 0)
IF (@@error > 0)
GOTO Quit
DELETE msdb.dbo.backupmediaset
FROM msdb.dbo.backupmediaset bms
WHERE bms.media_set_id IN (SELECT media_set_id
FROM #TempMediaSetID)
AND ((SELECT COUNT(*)
FROM msdb.dbo.backupset
WHERE media_set_id = bms.media_set_id) = 0)
DROP TABLE #TempBackupSetID
DROP TABLE #TempMediaSetID
DROP TABLE #TempRestoreHistoryID
IF (@@error > 0)
GOTO Quit
COMMIT TRANSACTION
RETURN
Quit:
ROLLBACK TRANSACTION
END