Delete Specific Backup file through SQL

backup, sql-server

Solution

May be old but might help someone. xp_delete_file can be used to delete specific backup file. Try the code below:

EXECUTE master.dbo.xp_delete_file 0,N'c:\backup\backup1.bak'

Problem

I've been researching about how to delete a specific backup file through an SQL query, but I only find results about "deleting backups older than a date". That is not what I want. I want to keep old backups, but I want to be able to delete a specific backup by its ID. I can easily remove the entries from the msdb tables and its restore history for a given backup, but I would like to be able to delete the files as well through an SQL query (I know their full path, as it is stored in the database), so that they don't keep wasting space in the disk. The procedure "xp_delete_file" doesn't seem to allow to delete a specific file. I assume that if there is a procedure to delete old files, there should be some way to delete a specific file. Please don't worry about security here.

Original source

Related problems