SQL statement used to delete an SQL Server backup file

Source: Internet
Author: User

After the project is launched, an automatic backup is created for each database on the database server to ensure the security of customer data. A new backup file is generated every day and stored in the backup folder, in addition, a sub-folder named by the database location is created in the folder, and the backup file is placed in this folder. After a long time, the server space is insufficient, originally, an OA database was very large, with 4 GB or so, which would consume about 4 GB of space every day. Therefore, we had to delete the previously backed up files every day for a long time, finally, this statement is obtained:

Exec xp_cmdshell 'forfiles/p c: backup/S/M *. bak/D-30/C "CMD/C del/Q/f @ file "'

/D-30 indicates the file 30 days ago,

/S indicates recursive subfolders search

*. Bak indicates querying all Bak-terminated file names

C: backup indicates that the root directory to be searched is c: \ backup.

 

Note that you must enable xp_cmdshell in the SQL Server peripheral configurator.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.