SQL: Guaranteed Way to Quickly Shrink the T-Log

My hero, Brad McGehee, has often advised against shrinking the transaction log when it's unnecessary. According to his (really amazing) SQL Server DBA Best Practices PowerPoint Presentation, shrinking too often can lead to increased file fragmentation and excessive use of resources allocated to grow/shrink the file.

Sometimes, however, it is necessary. Often times, getting the transaction log to shrink can be tricky but here's how you can do it. That is, unless the t-log is currently in use (think SharePoint Search crawls), then the query will just wait there until the transactions are complete.

DBCC SHRINKFILE(logicalfilename, 1) BACKUP LOG MyDatabase WITH TRUNCATE_ONLY DBCC SHRINKFILE(logicalfilename, 1)   If you don't know the logical filename, run sp_helpfile for a list of database filenames. Be sure to make a FULL backup of your database once the shrink is complete.

Update: This guy also has a great write-up on reasons to avoid shrinking the transaction log when possible.