DBCC SHRINKFILE (T-SQL)

Shrinks the size of the specified data file or log file for the related database.

Syntax

DBCC SHRINKFILE
    (    {file_name | file_id }
            {    [, target_size]
            |    [, {EMPTYFILE | NOTRUNCATE | TRUNCATEONLY}]
            }
    )

Arguments
file_name
Is the logical name of the file being shrunk. File names must conform to the rules for identifiers. For more information, see Using Identifiers.
file_id
Is the identification number (ID) of the file to be shrunk. To obtain a file ID, use the FILE_ID function or search sysfiles in the current database.
target_size
Is the desired size for the file in megabytes, expressed as an integer. If not specified, DBCC SHRINKFILE reduces the size as much as possible.

If target_size is specified, DBCC SHRINKFILE attempts to shrink the file to the specified size. Used pages in the part of the file to be freed are relocated to available free space in the part of the file retained. For example, if there is a 10-MB data file, a DBCC SHRINKFILE with a target_size of 8 causes all used pages in the last 2 MB of the file to be reallocated into any available free slots in the first 8 MB of the file. DBCC SHRINKFILE does not shrink a file past the size needed to store the data in the file. For example, if 7 MB of a 10-MB data file is used, a DBCC SHRINKFILE statement with a target_size of 6 shrinks the file to only 7 MB, not 6 MB.

EMPTYFILE
Migrates all data from the specified file to other files in the same filegroup. Microsoft® SQL Server™ no longer allows data to be placed on the file used with the EMPTYFILE option. This option allows the file to be dropped using the ALTER DATABASE statement.
NOTRUNCATE
Causes the freed file space to be retained in the files.

When NOTRUNCATE is specified along with target_size, the space freed is not released to the operating system. The only effect of the DBCC SHRINKFILE is to relocate used pages from above the target_size line to the front of the file. When NOTRUNCATE is not specified, all freed file space is returned to the operating system.

TRUNCATEONLY
Causes any unused space in the files to be released to the operating system and shrinks the file to the last allocated extent, reducing the file size without moving any data. No attempt is made to relocate rows to unallocated pages. target_size is ignored when TRUNCATEONLY is used.
Remarks

DBCC SHRINKFILE applies to the files in the current database. Switch context to the database to issue a DBCC SHRINKFILE statement referencing a file in that particular database. For more information about changing the current database, see USE.

The database cannot be made smaller than the size of the model database.

Use DBCC SHRINKFILE to reduce the size of a file to smaller than its originally created size. The minimum file size for the file is then reset to the newly specified size.

To remove any data that may be in a file, execute DBCC SHRINKFILE(‘file_name’, EMPTYFILE) before executing ALTER DATABASE.

The database being shrunk does not have to be in single-user mode; other users can be working in the database when the file is shrunk. You do not have to run SQL Server in single-user mode to shrink the system databases.

For log files, SQL Server uses target_size to calculate the target size for the entire log; therefore, target_size is the amount of free space in the log after the shrink operation. Target size for the entire log is then translated to target size for each log file. Unlike data files, the shrinking of log files is not immediate. Each log file is marked with the target size of the shrink operation. Each subsequent log backup or log truncation attempts to shrink the file and bring its size as close to the target size as possible. Because a log file can only be shrunk to a virtual log file boundary, it may not be possible to shrink a log file to a size smaller than the size of a virtual log file, even if it is not being used. For example, a database with a log file of 1 GB can have the log file shrunk to only 128 MB. For more information on when truncation occurs, see Truncating the Transaction Log. For more information about determining virtual log file sizes, see Virtual Log Files.

Result Sets

DBCC SHRINKFILE returns this result set (values may vary):

DbId   FileId CurrentSize MinimumSize UsedPages   EstimatedPages

------ ------ ----------- ----------- ----------- --------------

5      1      176         96          168         168

  

(1 row(s) affected)

  

DBCC execution completed. If DBCC printed error messages, contact your system administrator.

  

This table describes the columns in the result set.

Column name Description  
DbId Database identification number of the file SQL Server attempted to shrink.  
FileId The file identification number of the file SQL Server attempted to shrink.  
CurrentSize The number of 8-KB pages the file currently occupies.  
MinimumSize The number of 8-KB pages the file could occupy, at minimum. This corresponds to the minimum size or originally created size of a file.  
UsedPages The number of 8-KB pages currently used by the file.  
EstimatedPages The number of 8-KB pages that SQL Server estimates the file could be shrunk down to.  

Permissions

DBCC SHRINKFILE permissions default to members of the sysadmin fixed server role or the db_owner fixed database role, and are not transferable.

Examples

This example shrinks the size of a file named DataFil1 in the UserDB user database to 7 MB.

USE UserDB

GO

DBCC SHRINKFILE (DataFil1, 7)

GO

  

See Also
ALTER DATABASE sysfiles
FILE_ID DBCC
Physical Database Files and Filegroups  

  


(c) 1988-98 Microsoft Corporation. All Rights Reserved.