Skip to content

Instantly share code, notes, and snippets.

@ahmedtijaninet
Last active December 15, 2022 04:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahmedtijaninet/7d5559f865368e661f8a12c86301d101 to your computer and use it in GitHub Desktop.
Save ahmedtijaninet/7d5559f865368e661f8a12c86301d101 to your computer and use it in GitHub Desktop.

To shrink the size of an LDF (log) file in SQL Server, you can use the DBCC SHRINKFILE command. This command takes the name of the log file as a parameter and tries to free up unused space in the file, reducing its size on disk. Here is an example of how you would use this command to shrink the size of a log file named my_log_file:

sql DBCC SHRINKFILE ('my_log_file', 10);

In this example, the second parameter specifies the target size for the log file, in megabytes. You can adjust this value as needed to achieve the desired size for the file. Note that the DBCC SHRINKFILE command can be time-consuming and resource-intensive, and it may not always be successful in reducing the size of the log file.

Another option is to use the BACKUP LOG command to create a backup of the log file, and then use the TRUNCATE_ONLY option of the BACKUP LOG command to truncate the log file and free up space. Here is an example of how you would use these commands to reduce the size of the log file:

sql BACKUP LOG [my_database] TO DISK = 'C:\backups\my_database.bak' WITH TRUNCATE_ONLY;

In this example, the BACKUP LOG command creates a backup of the my_database database and saves it to the specified file location. The TRUNCATE_ONLY option truncates the log file for the database and frees up space, but it does not create a full backup of the database. This approach can be more efficient than using the DBCC SHRINKFILE command, but it will only work if the database is in the "simple" recovery model.

Finally, you can change the recovery model for the database to either the "bulk-logged" or "full" recovery model, and then perform regular log backups using the BACKUP LOG command. This will allow you to truncate the log file on a regular basis and keep its size under control. You can use the following script to change the recovery model for a database and create a scheduled job to perform regular log backups:

ALTER DATABASE [my_database] SET RECOVERY BULK_LOGGED;
EXEC msdb.dbo.sp_add_job
  @job_name = N'Log Backup Job',
  @enabled = 1,
  @notify_level_eventlog = 0,
  @notify_level_email = 0,
  @notify_level_netsend = 0,
  @notify_level_page = 0,
  @delete_level = 0,
  @description = N'This job performs regular log backups for the my_database database.',
  @category_name = N'[Uncategorized (Local)]',
  @owner_login_name = N'sa';
EXEC msdb.dbo.sp_add_jobstep
  @job_name = N'Log Backup Job',
  @step_name = N'Log Backup Step',
  @subsystem = N'TSQL',
  @command = N'BACKUP LOG [my_database] TO DISK = ''C:\backups\my_ ```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment