Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete Specific Backup file through SQL

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.

like image 416
Nuno Avatar asked Apr 24 '13 09:04

Nuno


People also ask

Can I delete SQL BAK files?

You are free to delete . bak files as you please. They are not involved the actual running of SQL Server itself and wont prevent it starting up. I would suggest you create and schedule a maintenace plan in SQL Server Management Studio.

How do you delete specific data from a database?

To remove one or more rows in a table: First, you specify the table name where you want to remove data in the DELETE FROM clause. Second, you put a condition in the WHERE clause to specify which rows to remove. If you omit the WHERE clause, the statement will remove all rows in the table.

Which SQL command to remove records in SQL?

The Delete command in SQL is a part of the Data Manipulation Language, a sub-language of SQL that allows modification of data in databases. This command is used to delete existing records from a table. Using this, you can either delete specific records based on a condition or all the records from a table.


1 Answers

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'
like image 134
Vignesh Devan Avatar answered Oct 17 '22 21:10

Vignesh Devan