I experienced into one issues for database backups were failed. And this was due to space issues on disk drive. This disk drive is spacific to allocated for the database backups only.
The space was eaten by this database backups and this drive contains so many old backups. I have manually deleted all files and continue this activity so many days.If you create a maintenance plans then it have option to delete old backups files. But i have the stored procedure for the database backups. So i do not have option to delete old and unused database backup files.
Finally, I have created a script to clean those old backups. I have created one stored procedure in which you need to pass there parameters, One is Backup type as want to delete full, differential or transaction log backups. Second is From days and third one is End day.
Please make sure XP_CMDSHELL is enabled in database instance as thisis require to enable it to delete database backup files to be deleted physically. Here is the query to enable it.
USE MASTER
GO
EXEC SP_CONFIGURE 'show advanced options',1
GO
EXEC SP_CONFIGURE 'XP_CMDSHELL',1
GO
RECONFIGURE
GO
I have already told you as i have created script to delete the old DATABASEPROPERTY backups, please find below SP for the same.
SET ansi_nulls ON
go
SET quoted_identifier ON
go
CREATE PROCEDURE [dbo].[Deletedbbackupoldfiles] @BackupType CHAR(1) = 'D'
,-- 'D'-Full, 'I'-Differential, 'L'-Log backup type
@StartDayFromToday INT,
@EndDayFromToday INT
AS
BEGIN
SET nocount ON
DECLARE @IsFileExists INT
DECLARE @DeletedFile VARCHAR(500)
DECLARE @OldFiles VARCHAR(500)
DECLARE oldfiles CURSOR fast_forward FOR
SELECT bmf.physical_device_name
FROM msdb.dbo.backupset bs
INNER JOIN msdb.dbo.backupmediafamily bmf
ON ( bs.media_set_id = bmf.media_set_id )
WHERE device_type = 2
AND type = @BackupType
AND backup_start_date < = Getdate() - @StartDayFromToday
AND backup_start_date > = Getdate() - @EndDayFromToday
-- AND DATEDIFF(DAY,BACKUP_START_DATE,GETDATE()) > BETWEEN @StartDayFromToday and @EndDayFromToday
OPEN oldfiles
FETCH next FROM oldfiles INTO @OldFiles
WHILE @@FETCH_STATUS = 0
BEGIN
EXEC Xp_fileexist
@OldFiles,
@IsFileExists output
IF @IsFileExists = 1
BEGIN
PRINT 'Deleted File : ' + @DeletedFile
SET @DeletedFile = 'DEL ' + @OldFiles
EXEC Xp_cmdshell
@DeletedFile
END
FETCH next FROM oldfiles INTO @OldFiles
END
CLOSE oldfiles
DEALLOCATE oldfiles
END
Finally after creating a stored procedures , it is time to run and clean the old database backups. After running it will remove all the old database backups as per specified parameters. Like we have passed 'D',3 and 10 with SP, So i will delete Full database backups which are older then 3 days ago and 10 days before created.
EXEC [DeleteDBBackupOldFiles]
@BackupType = 'D',
@StartDayFromToday = 3,
@EndDayFromToday = 10
Which method you are using for old database backup maintenance? You can read my earlier posts for delete files using
File System Task and
Script Task in SSIS.