SQL Server How To: Shrink All SQL Server Database Log Files

Peter Musu`
1 min readJul 17, 2020

Run the following command and then copy the results. Comment out any databases you do not want to shrink.

Note this only shrinks the log files of the databases.

set nocount on SELECT ‘USE [‘ + d.name + N’]’ + CHAR(13) + CHAR(10) + ‘DBCC SHRINKFILE (N’’’ + mf.name + N’’’ , 0, TRUNCATEONLY)’ + CHAR(13) + CHAR(10) + CHAR(13) + CHAR(10) FROM sys.master_files mf JOIN sys.databases d ON mf.database_id = d.database_id WHERE d.database_id > 4 and mf.type_desc = ‘LOG’

--

--