Home > SQL Server Job Management > Delete specific file by use of Sql server xp_cmdshell command

Delete specific file by use of Sql server xp_cmdshell command


Topic: Delete specific file by use of Sql server xp_cmdshell command

 

We are use centralize backup server to keep the backup and required some maintenance to delete all the old file. Here use the below script to delete the backup file by use of xp_cmdshell command from sql server.

 

Delete all .sql files in the C:\Backup directory and its subfolders where the file modified date is older than 1/20/2013.

 

EXEC xp_cmdshell ‘FORFILES /p c:\BACKUP /s /m *.sql /d 1/20/2013 /c “CMD /C del /Q /F @FILE”‘

 

Delete all .sql files in the C:\Backup directory and its subfolders where the file modified date is more than 30 days old.

 

EXEC xp_cmdshell ‘FORFILES /p c:\BACKUP /s /m *.sql /d -30 /c “CMD /C del /Q /F @FILE”‘

 

Delete all .sql files in the C:\Backup directory and its subfolders where the file modified date is more than 30 days old and the file name starts with an “F_”.

 

EXEC xp_cmdshell ‘FORFILES /p c:\BACKUP /s /m F_*.sql /d -30 /c “CMD /C del /Q /F @FILE”‘

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: