locked
How to use FORFILES Command to Delete SQL Server Backups? RRS feed

  • Question

  •  Recently I constructed a new backup process that I want to institute globally across all my SQL Server 2005 instances, Is there a way I can automate a file deletion process from within SQL Server?
    Saturday, September 19, 2015 4:32 AM

Answers

  • How have you constructed backup process is it by Maintenance plan. If so in the same maintenance plan you have Maintenance plan cleanup task which deletes the backup files after specified days.

    You can also use script to delete backup files please read this thread


    Please mark this reply as answer if it solved your issue or vote as helpful if it helped so that other forum members can benefit from it

    My Technet Wiki Article

    MVP

    • Marked as answer by Jason_Clark03 Saturday, September 19, 2015 5:51 AM
    Saturday, September 19, 2015 5:15 AM

All replies

  • How have you constructed backup process is it by Maintenance plan. If so in the same maintenance plan you have Maintenance plan cleanup task which deletes the backup files after specified days.

    You can also use script to delete backup files please read this thread


    Please mark this reply as answer if it solved your issue or vote as helpful if it helped so that other forum members can benefit from it

    My Technet Wiki Article

    MVP

    • Marked as answer by Jason_Clark03 Saturday, September 19, 2015 5:51 AM
    Saturday, September 19, 2015 5:15 AM
  • The FORFILES command will select a subset of files and execute a command against the set. The command requires the following parameters and accepts the following variables: /p, /m, /s, /c, /d.

    Using these parameters the following examples could be constructed to take care of your dilemma for deleting your backup script files.  You can create scripts based upon modification date/time or backup type.  You can even construct scripts that utilize both criteria.  We will now take a closer look at these potential scripts.  Remember that you will be executing these from within T-SQL code, so you will need to wrap the statements within an xp_cmdshell call in the format of EXEC xp_cmdshell 'FORFILES COMMAND'. Please note that in all examples I am using the /Q and /F flags for the del command.  These signify that the command will use quiet mode (/Q) and will even delete read-only files (/F).


    Saturday, September 19, 2015 6:01 AM