This article is a mirror article of machine translation, please click here to jump to the original article.

View: 11276|Reply: 4

[Source] SQL Server deletes backup bak files regularly

[Copy link]
Posted on 1/31/2019 1:11:24 PM | | | |
Generally, when we do database backup, we only need to back up a certain period of time in the past, and we can't use backup files that are too old.

Since the backup file is too large and too many database backup files take up too much disk space, how to delete the backup file regularly?

First, the code:

Everyone makes changes according to their actual situation.

The following is an example of SQL Server 2012, please test other versions by yourself, there should be no problem.

Before execution:



After execution:



You can see that our backup file has been deleted, just add the sql script to the scheduled task to execute, as for how to add it to the scheduled task, I won't say.

(End.)




Previous:.net core FileProvider reads directory files
Next:=> is a Lambda expression used to create a default delegation
 Landlord| Posted on 10/27/2025 3:38:28 PM |
Procedure for the daily automatic backup operation of SQL Server databases
https://www.itsvse.com/thread-4895-1-1.html
 Landlord| Posted on 10/27/2025 3:46:02 PM |
To fully back up the database, the sql command is as follows:

YourDatabaseName: Replace with the actual name of the database.
DISK = '/var/opt/mssql/data/YourDatabaseName_20251027.bak': Specify the destination path and file name of the backup file.
WITH FORMAT: Initializes the backup media to overwrite any existing backup set on the specified device.
COMPRESSION: Compress backups to reduce their size. (Note: Not all SQL Server editions support compression, such as SQL Server Express.)
STATS = 10: A progress message is displayed every 10% of the backup operation.
 Landlord| Posted on 12/2/2025 11:14:45 AM |
Xiaozha Posted on 2025-10-27 15:46
To fully back up the database, the sql command is as follows:

YourDatabaseName: Replace with the actual name of the database.

View the logical names and physical paths of the data files and log files contained in the backup file. The command is as follows:
 Landlord| Posted on 12/2/2025 3:31:09 PM |
xp_delete_file

xp_delete_file is an undocumented extended stored procedure in SQL Server, and while it does exist and is used by some maintenance tasks, Microsoft does not recommend calling it directly in production because it is not supported by official documentation and may be removed or changed in future versions.

xp_delete_file The following 5 parameters:

  • File Type = 0 for backup files and 1 for report files.
  • Folder Path = The folder where you want to delete the file. The path must end with a backslash.
  • File Extension = This can be "BAK" or "TRN" or whatever extension you normally use.
  • Date = The due date of the file that needs to be deleted.
  • Subfolder = 0 means ignore the subfolder and 1 means delete the files in the subfolder.

Reference:The hyperlink login is visible.

xp_delete_files

In SQL Server 2019 a more powerful function sys.xp_delete_files, he saidSupport for standard DOS wildcards, can be used in combination with sys.dm_os_enumerate_filesystem.

Reference:The hyperlink login is visible.
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com