This article describes how to shrink a data or log file of a database in SQL Server by using SQL Server Management Studio or Transact-SQL.
Transaction log growth can occur for a few different reasons. Long running transactions, incorrect recovery model configuration and lack of log backups can grow the log.
Typically, truncation occurs automatically under the simple recovery model when database is backed up and under the full recovery model when the transaction log is backed up.
Log truncation frees up space in the log file so the transaction log can reuse it. Let’s see two ways to shrink the log file.
Shrink the Log in SQL Server Management Studio
To shrink the log in SSMS, right click the database, choose Tasks -> Shrink -> Files:
On the Shrink File window, select the File type to Log. You can also choose to either release unused space, reorganize pages before releasing unused space, or empty file by migrating the data to other files in the same file group:
DBCC SHRINKFILE
Shrinks the size of the specified data or log file for the current database, or empties a file by moving the data from the specified file to other files in the same file group, allowing the file to be removed from the database. You can shrink a file to a size that is less than the size specified when it was created. This resets the minimum file size to the new value.
If the database is in FULL recovery model you could set it to SIMPLE, run DBCC SHRINKFILE, and set back to FULL.
Provide the logical name of the log file and the target size in MB you need to shrink.
USE [VMCRT8030ITE]
GO
-- Truncate the log by changing the database recovery model to SIMPLE.
ALTER DATABASE VMCRT8030ITE
SET RECOVERY SIMPLE;
GO
-- Shrink the truncated log file to 1 MB.
DBCC SHRINKFILE (log01, 1);
GO
-- Reset the database recovery model.
ALTER DATABASE VMCRT8030ITE
SET RECOVERY FULL;
GO
You can find the logical name of the log file of your database by using the following query:
USE [VMCRT8030ITE]
GO
SELECT name FROM sys.master_files WHERE type_desc = 'LOG' and database_id = (select DB_ID())
Security and Permissions
Requires membership in the sysadmin fixed server role or the db_owner fixed database role.
References
https://docs.microsoft.com/en-us/sql/t-sql/database-console-commands/dbcc-shrinkfile-transact-sql