Shrink All Physical Files of a Database

Home / Shrink All Physical Files of a Database

Using setvar in T-SQL
Set the values of the two SQLCMD variables.

In this example, the script will only run the shrink if the ‘foo’ database has at least 100 MB to potentially free.

The script runs a shrink on each file of the database until the reduction value drops to zero.

Note: We use the “Simple” recovery mode for non-production databases, and ‘Full’ for those in production. This may affect how much the transaction log size can be reduced, specifically, the time since the last SQL backup may affect the trans-log’s shrinkability when the database is set to use ‘Full’ recovery mode.

Here’s the same thing as a stored procedure.