Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Delete old Backup files using T-sql script


Delete old Backup files using T-sql script

Author
Message
Ignacio A. Salom Rangel
Ignacio A. Salom Rangel
Hall of Fame
Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)

Group: General Forum Members
Points: 3114 Visits: 1439
Which version of SQL Server are you running?




My blog

Gordon-265412
Gordon-265412
SSC-Enthusiastic
SSC-Enthusiastic (184 reputation)SSC-Enthusiastic (184 reputation)SSC-Enthusiastic (184 reputation)SSC-Enthusiastic (184 reputation)SSC-Enthusiastic (184 reputation)SSC-Enthusiastic (184 reputation)SSC-Enthusiastic (184 reputation)SSC-Enthusiastic (184 reputation)

Group: General Forum Members
Points: 184 Visits: 1188
This is a procedure I wrote some time ago to delete files from a fileshare older than n-days.


IF EXISTS (SELECT [name] FROM dbo.sysobjects WHERE [name] = 'usp_Admin_Delete_Files_By_Date' AND TYPE = 'P')
   DROP PROCEDURE dbo.usp_Admin_Delete_Files_By_Date
GO

CREATE PROCEDURE dbo.usp_Admin_Delete_Files_By_Date (@SourceDir varchar(1024), @SourceFile varchar(512), @DaysToKeep int)
-- EXEC Admin.dbo.usp_Admin_Delete_Files_By_Date @SourceDir = '\\FooServer\BarShare\'
--                      , @SourceFile = 'FooFile_*'
--                      , @DaysToKeep = 3

AS

/******************************************************************************
**
**   Name: usp_Admin_Delete_Files_By_Date.sql
**
**   Description: Delete files older than X-days based on path & extension.
**
**      Depending on the output from xp_msver, we will execute either a
**      Windows 2000 or Windows 2003 specific INSERT INTO #_File_Details_02
**      operation as there is a small difference in the FOR output between
**      Windows 2000 and 2003 (Operating system versions).
**
**   Return values: 0 - Success
**         -1 - Error
**
**   Author: G. Rayburn
**
**   Date: 03/26/2007
**
**   Depends on: xp_cmdshell access to @SourceDir via SQLAgent account.
**
*******************************************************************************
**      Modification History
*******************************************************************************
**
**   Initial Creation: 03/26/2007 G. Rayburn
**
*******************************************************************************
**
******************************************************************************/
SET NOCOUNT ON

DECLARE @CurrentFileDate char(10)
   , @OldFileDate char(10)
   , @SourceDirFOR varchar(255)
   , @FileName varchar(512)
   , @DynDelete varchar(512)
   , @ProcessName varchar(150)
   , @OSVersion decimal(3,1)
   , @Error int


SET @ProcessName = 'usp_Admin_Delete_Files_By_Date - [' + @SourceFile + ']'
SET @CurrentFileDate = CONVERT(char(10),getdate(),121)
SET @OldFileDate = CONVERT(char(10),DATEADD(dd,-@DaysToKeep,@CurrentFileDate),121)
SET @SourceDirFOR = 'FOR %I IN ("' + @SourceDir + @SourceFile + '") DO @ECHO %~nxtI'
SET @Error = 0


-- Get Windows OS Version info for proper OSVer statement block exec.
CREATE TABLE #_OSVersion
   ( [Index] int
   , [Name] varchar(255)
   , [Internal_Value] varchar(255)
   , [Character_Value] varchar(255) )

INSERT INTO #_OSVersion
   EXEC master..xp_msver 'WindowsVersion'

SET @OSVersion = (SELECT SUBSTRING([Character_Value],1,3) FROM #_OSVersion)



-- Start temp table population(s).
CREATE TABLE #_File_Details_01
   ( Ident int IDENTITY(1,1)
   , Output varchar(512) )

INSERT INTO #_File_Details_01
   EXEC master..xp_cmdshell @SourceDirFOR

CREATE TABLE #_File_Details_02
   (Ident int
   , [TimeStamp] datetime
   , [FileName] varchar(255) )


-- OS Version specifics.
IF @OSVersion = '5.0'
   BEGIN -- Exec Windows 2000 version.
      INSERT INTO #_File_Details_02
         SELECT Ident
            , CONVERT(datetime, LEFT(CAST(SUBSTRING([Output],1,8) AS datetime),12)) AS [TimeStamp]
            , SUBSTRING([Output],17,255) AS [FileName]
            FROM #_File_Details_01

         WHERE [Output] IS NOT NULL
         ORDER BY Ident
   END

IF @OSVersion = '5.2'
   BEGIN -- Exec Windows 2003 version.
      INSERT INTO #_File_Details_02
         SELECT Ident
            , CONVERT(char(10), SUBSTRING([Output],1,10), 121) AS [TimeStamp]
            , SUBSTRING([Output],21,255) AS [FileName]
            FROM #_File_Details_01
   
         WHERE [Output] IS NOT NULL
         ORDER BY Ident
   END



-- Start delete ops cursor.
DECLARE curDelFile CURSOR
READ_ONLY
FOR

   SELECT [FileName]
      FROM #_File_Details_02
   WHERE [TimeStamp] <= @OldFileDate

OPEN curDelFile

FETCH NEXT FROM curDelFile INTO @FileName
WHILE (@@fetch_status <> -1)
BEGIN
   IF (@@fetch_status <> -2)
   BEGIN

      SET @DynDelete = 'DEL /Q "' + @SourceDir + @FileName + '"'

      EXEC master..xp_cmdshell @DynDelete

   END
   FETCH NEXT FROM curDelFile INTO @FileName
END

CLOSE curDelFile
DEALLOCATE curDelFile

DROP TABLE #_OSVersion
DROP TABLE #_File_Details_01
DROP TABLE #_File_Details_02
GO



Your friendly High-Tech Janitor... Smile
alee-652255
alee-652255
Grasshopper
Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)

Group: General Forum Members
Points: 18 Visits: 61
This is how I keep backup for seven days:

Use Master
Declare
@new_device nvarchar(40),
@new_file nvarchar(40),
@old_device nvarchar(40)

set @new_device='MyDB'+convert(char(8),getdate(),12)
set @new_file='d:\mssql\BACKUP\MyDB'+convert(char(8),getdate(),12)
set @old_device='MyDB'+convert(char(8),(getdate()-7),12)

EXEC sp_addumpdevice 'disk',@new_device,@new_file
BACKUP DATABASE MyDB TO @new_device

If exists (Select name from sysdevices
where name=@old_device)
EXEC sp_dropdevice @old_device,delfile
Ivanna Noh
Ivanna Noh
SSC Eights!
SSC Eights! (955 reputation)SSC Eights! (955 reputation)SSC Eights! (955 reputation)SSC Eights! (955 reputation)SSC Eights! (955 reputation)SSC Eights! (955 reputation)SSC Eights! (955 reputation)SSC Eights! (955 reputation)

Group: General Forum Members
Points: 955 Visits: 2906
have you checked to ensure that the owner of the maintenance job (the identity that it runs under) has delete permission on the server folder where the backups are stored?
krammana
krammana
SSC Journeyman
SSC Journeyman (93 reputation)SSC Journeyman (93 reputation)SSC Journeyman (93 reputation)SSC Journeyman (93 reputation)SSC Journeyman (93 reputation)SSC Journeyman (93 reputation)SSC Journeyman (93 reputation)SSC Journeyman (93 reputation)

Group: General Forum Members
Points: 93 Visits: 121
You Can use this Extended stored procedure for that....master.dbo.xp_delete_file
nivas
nivas
Grasshopper
Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)

Group: General Forum Members
Points: 17 Visits: 50
Hi in this case my date format used in backup file getutcdate() format but

to delete files i am using this script

Declare @sql varchar(250),@retention tinyint
set @retention=0
set @sql='Exec master.dbo.xp_cmdshell ''Del E:\SQL2005\Backup\*' + convert(varchar(15),getdate()-@retention,120) + '*.bak'''
Exec (@sql)

i also tried with getutcdate()



but it gives the following error.

Could Not Find \\E:\SQL2005\Backup\*2008-04-03*.bak

why i donot understand.

can any one give heads up.
alee-652255
alee-652255
Grasshopper
Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)

Group: General Forum Members
Points: 18 Visits: 61
Do not use mapped drive path, use UNC path.
Ola Hallengren
Ola Hallengren
SSC-Enthusiastic
SSC-Enthusiastic (172 reputation)SSC-Enthusiastic (172 reputation)SSC-Enthusiastic (172 reputation)SSC-Enthusiastic (172 reputation)SSC-Enthusiastic (172 reputation)SSC-Enthusiastic (172 reputation)SSC-Enthusiastic (172 reputation)SSC-Enthusiastic (172 reputation)

Group: General Forum Members
Points: 172 Visits: 1938
The Maintenance Plans are using the extended stored procedure xp_delete_file to delete files. That can also be used in T-SQL code.

EXECUTE xp_delete_file 0, 'C:\Backup', 'bak', '2008-04-18T00:00:00'

I have a backup stored procedure that is using xp_delete_file that you can use if you like.

http://ola.hallengren.com/sql-server-backup.html

Ola Hallengren
http://ola.hallengren.com
nivas
nivas
Grasshopper
Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)

Group: General Forum Members
Points: 17 Visits: 50
i didnot find any script or solution from this link you provided.
sorry
nivas
nivas
Grasshopper
Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)

Group: General Forum Members
Points: 17 Visits: 50
Actually i am using UNC Path, however i tried again today but this time it is asking comformation like are you sure y
for this case what i have to do.
advise
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search