Shrink database sql server

This note is just to remind me about shrinking database file to get more free space from database log. Step by step is:

Backup full database and database related
Backup database transaction log
Run code below for checking current database and log size. With this code, we also can get database log name that will be used.

SELECT
–DB_NAME(database_id) AS DatabaseName,
–Physical_Name,
Name AS Logical_Name,
(size*8)/1024 SizeMB
FROM
sys.master_files
WHERE
DB_NAME(database_id) = ‘tempdb’ — database name that will be Shrinked (change)
GO

Run this code for shrinking database:

USE tempdb;
GO
— Truncate the log by changing the database recovery model to SIMPLE.
ALTER DATABASE tempdb
SET RECOVERY SIMPLE WITH NO_WAIT;
GO
— Shrink the truncated log file to 1 MB.
DBCC SHRINKFILE(tempdb_log, 1); –file_name is the logical name of the file to be shrink
GO
— Reset the database recovery model.
ALTER DATABASE tempdb
SET RECOVERY FULL WITH NO_WAIT;
GO

Done