0

I am using a SQL-Server 2012 (SP2) on a WinServer 2012 R2. I created a maintenance plan that makes a backup of my databases and deletes report files and backup files that are older than 2 days.

My problem is that the maintenance plan doesn't delete files which are older than 2 days. these files are also not deleted after 5 or 6 days. The backups are verified and compressed.

I have rights on the directory (agent is running under LocalSystem) and in the task properties under "file extension" I tried to set the name without and with a dot, e.g. ".bak", "bak" and "*.bak" but nothing works. I changed the order of the task and nothing works.

But I always get success messages. So what is here wrong?

Murat Yazi
  • 85
  • 1
  • 8

2 Answers2

0

Just to clarify, are you trying to run a script (i.e., .bat) to handle some of this through SQLCMD, and calling that script through Task Scheduler? From your question, it sounds like you've set the Maintenance Plan up in SQL Server, but are trying to setup a "Task" to call it (Task Scheduler), rather than a "Job" (SQL Agent), my apologies if this is not the case.

It will be a lot tidier to do this through the SQL Agent (the SQL Server internal equivalent of Task Scheduler). Once you have a Maintenance Paln you can test through the Management Studio Interface, it's a breeze to add the Agent Job.

If you are running a Task Scheduler Task to call a script, it is likely you're getting "success" because the Task started your script, but the script isn't setup to handle the returns from the SQLCMD call properly.

Aleyn
  • 1
  • 1
  • No I am just using the maintenance plan. These different jobs in the maintenance plan are called tasks, so I think I was clear enough. https://msdn.microsoft.com/en-us/library/hh710041.aspx – Murat Yazi Jan 31 '16 at 23:27
  • In your original question, you're using the example file extension "bat". As I'm sure you know, this extension is used for Windows Command Prompt batch files, and I think it's understandable that this might lead a reader who doesn't know exactly what you mean to think your issue might have something to do with scripting commands. The default extension for SQL Server backup files is "bak". Unless you've specifically changed it to bat in your backup task, or you repeated the same typo in your original question 3 times, this could be the cause of your issue. – Aleyn Feb 01 '16 at 00:36
  • Thank you, I corrected this mistake. I meant .BAK and not .BAT Don't know why I wrote bat... :D – Murat Yazi Feb 01 '16 at 12:49
0

I have tried to solve it in different ways and there has been no way, the fastest I have found is to use a .bat that deletes them and schedule a windows task that does it every night.

This bat clears from the last 3 days :

FORFILES /P "C:\Directorio" /S /D -3 /c "CMD /c DEL /Q @PATH"