Link to home
Start Free TrialLog in
Avatar of vijay11
vijay11

asked on

how to delete a .bak file using a job in sqlserver

how to delete a .bak file using a job in sqlserver.let us assume the backup is in c:\software.i have to delete .bak file.can some one tell the command.i have to keep it in the job.
Avatar of Raja Jegan R
Raja Jegan R
Flag of India image

You can either use xp_cmdshell command to delete your *.bak files from the specified path or if you are taking regular backups using maintenance plans, then you can specify the expiry set for the backup files so that age old files would be deleted automatically by the Maintenance plan itself.
Avatar of vijay11
vijay11

ASKER

i want this in sqlserver 2000.let ud suppose the files are in c:\software and i have to delete files older than 20 hrs.can you please write the command
Vijay,

Try using a maintenance plan, configuring the CleanUp History section of it. It's very easy to do.

Rob
Avatar of vijay11

ASKER

if we use maintainance plan(sql 2000).we can not delete backups untill the backup jobs are totally completed.i want a script using xp_cmdshell to delete back ups older than 20 hrs
Is PowerShell okay? Otherwise, you'll need VBScript, which I'm less keen on these days.

dir c:\software\*.bak | where-object -filter {$_.LastWriteTime -lt (Get-Date).AddHours(-20)} | % {$_.Delete()}

Hope this helps,

Rob
ASKER CERTIFIED SOLUTION
Avatar of RiteshShah
RiteshShah
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial