Delete DB after "x" Days

  • Hello,

    I am creating a Scheduled JOb that will delete backups older than 3 days old, however I seem to be missing something in my script. Can someone help me understand what I am doing wrong?

    DECLARE @DeleteDate datetime

    SET @DeleteDate = DateAdd(day, -3, GetDate())

    EXECUTE master.dbo.xp_delete_file 0,N'E:\BackUps\',N'bak',N' + @DeleteDate + ''

  • Why do you think something's missing? Is it giving an error? Not working? Something else?

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • Hi,

    Thank you for you response. I get the following error message:

    Msg 22049, Level 15, State 0, Line 0

    Error executing extended stored procedure: Invalid Parameter

    So it seems my syntax is off. Also I realized by original post is missing a single quote. Here is the correct code:

    DECLARE @DeleteDate datetime

    SET @DeleteDate = DateAdd(day, -3, GetDate())

    EXECUTE master.dbo.xp_delete_file 0,N'E:\Travel_Baks\',N'bak',N' + @DeleteDate + '''

  • It looks like you're trying to pass in the date parameter as a character string. I don't think that's necessary. Have you tried doing this?

    DECLARE @DeleteDate datetime

    SET @DeleteDate = DateAdd(day, -3, GetDate())

    EXECUTE master.dbo.xp_delete_file 0,N'E:\Travel_Baks\',N'bak', @DeleteDate

  • thank you, that was it!

  • 🙂

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply