Nin
Nin

Reputation: 31

How to delete a newly created SQL server database backup file in c#?

I'm creating a desktop application that backs up SQL server database periodically and sends the backup as attachment over email. After every iteration, I modify the file name to look like Backup, Backup_1, Backup_2 etc., to identify the files at the receiving end.

In the scenario where creating backup is successful (say Backup_1), but sending email has failed, I would like to delete the backup (Backup_1), so that I can reuse the same name in next iteration. In this way there would not be any gaps in the receiving end.

Unfortunately, when I delete the backup file, I'm getting the exception: "The process cannot access the file 'D:\Backups\Backup_1.bak' because it is being used by another process." I'm not aware of any other process that can access this file.

Please advise on how to tackle this issue.

I tried running as administrator, closing SQL Server Management Studio, but nothing helped. How do I find the process that's blocking me?

Upvotes: 0

Views: 161

Answers (1)

Nin
Nin

Reputation: 31

Apparently it was the email sending part, that was holding reference to the attachment. Disposing the email attachments in both successful and failure scenarios did the trick. Also, I moved away from this approach to sending backups as json attachments and adding in server database as it gave me more flexibility.

Upvotes: 0

Related Questions