T-SQL for Delete backups

How to write T-SQL to compare two existing sql backups (.bak) in a given location and delete old backups?
This is sql server 2016?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kelvin SparksCommented:
I use Powershell for jobs like that. Can delete files in a folder (can also drill down to sub folders) to delete foles older thata set number of days.

Happy to provide the applet if you're interested.

Ajay ChananaMCSE-2003/08|RHCSA| VCP5/6 |vExpert2018Commented:
Some what i agree with Kelvin, as SQL will only delete if the database is mounted to SQL.

Although you may create procedure in SQL with below script and schedule it for deleting old backup.

CREATE PROCEDURE [dbo].[usp_DeleteOldBackupFiles] @path NVARCHAR(256),
      @extension NVARCHAR(10),
      @age_hrs INT

      DECLARE @DeleteDate NVARCHAR(50)
      DECLARE @DeleteDateTime DATETIME

      SET @DeleteDateTime = DateAdd(hh, - @age_hrs, GetDate())

        SET @DeleteDate = (Select Replace(Convert(nvarchar, @DeleteDateTime, 111), '/', '-') + 'T' + Convert(nvarchar, @DeleteDateTime, 108))

      EXECUTE master.dbo.xp_delete_file 0,

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
VijayAuthor Commented:
Hi Ajay,

Let me try and get back to you.
The 7 Worst Nightmares of a Sysadmin

Fear not! To defend your business’ IT systems we’re going to shine a light on the seven most sinister terrors that haunt sysadmins. That way you can be sure there’s nothing in your stack waiting to go bump in the night.

Mark WillsTopic AdvisorCommented:
Why not set up a maintenance plan to "set and forget" once you are happy with it of course...

Read : https://docs.microsoft.com/en-us/sql/relational-databases/maintenance-plans/use-the-maintenance-plan-wizard

Or walkthrough : https://dataginger.com/2013/08/07/sql-server-automating-backup-files-cleanup-task-using-maintenance-plans/

Includes disk cleanup...
VijayAuthor Commented:
Mark  Yes. We already configured maintenance plans . But this requirement  is for some testing.
Mark WillsTopic AdvisorCommented:
No worries, misunderstood the requirements.

Will leave you in the hands of the experts :)

One question, if the backups are going to the same location, I assume the only real comparison is Date of the file on disk ? Or is it time period ?

You might want to get a list of BAK files before doing : EXEC master.sys.xp_delete_file 0,@path,'BAK',@DeleteDate,0;

It is an undocumented procedure so you do need to take care....

Check out : https://voiceofthedba.com/2017/10/09/using-xp_delete_file/
Vitor MontalvãoMSSQL Senior EngineerCommented:
What do you want to compare?
If they are the same backup file? Then check the file names, sizes and date and time.
Vitor MontalvãoMSSQL Senior EngineerCommented:
Vijay, please let us know if this issue has been solved or if you need further help.
VijayAuthor Commented:
Thank you very much Virot
VijayAuthor Commented:
Thank you Vitor.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.