The shrink command is used to reduce the physical log file size of a database.
NOTE: That can be understood as de-fragmentation, file compaction, and resetting file physical pointers.
Often databases that are setup with the simple recovery model have corresponding backup jobs that would run during non-business hours (e.g. 2AM) as this will automatically shrink log files.
Other recovery models such as FULL and BULK-Logged will require special considerations.
WARNING: There is an inherent risk of data loss should this process fails.
Here is a sample T-SQL to perform this task on a Full-Recovery-Model database:
USE [master] GO ALTER DATABASE [TestDb] SET RECOVERY SIMPLE WITH NO_WAIT DBCC SHRINKFILE (TestDb, 1) ALTER DATABASE [TestDb] SET RECOVERY FULL WITH NO_WAIT GO
The truncate command changes the status of one or more virtual log files (VLF) from active to inactive, which marks those files for overwriting or reuse.
NOTE: The truncate command can also be substituted with NO_LOG, which will purge a database’s transaction log.
Here is a sample T-SQL to perform this task manually:
USE [master] GO DBCC SHRINKFILE(TestDb_Log, 1) BACKUP LOG TestDb WITH TRUNCATE_ONLY -- NO_LOG (to delete log file) DBCC SHRINKFILE(TestDb_Log, 1) GO
NOTE: