Shrink a database - SQL Server (2024)

  • Article

Applies to: Shrink a database - SQL Server (1) SQL Server Shrink a database - SQL Server (2) Azure SQL Database Shrink a database - SQL Server (3) Azure SQL Managed Instance

This article describes how to shrink a database in SQL Server by using Object Explorer in SQL Server Management Studio or Transact-SQL.

Shrinking data files recovers space by moving pages of data from the end of the file to unoccupied space closer to the front of the file. When enough free space is created at the end of the file, data pages at end of the file can be deallocated and returned to the file system.

Limitations and restrictions

  • The database can't be made smaller than the minimum size of the database. The minimum size is the size specified when the database was originally created, or the last explicit size set by using a file-size-changing operation, such as DBCC SHRINKFILE. For example, if a database was originally created with a size of 10 MB and grew to 100 MB, the smallest size the database could be reduced to is 10 MB, even if all the data in the database has been deleted.

  • You can't shrink a database while the database is being backed up. Conversely, you can't back up a database while a shrink operation on the database is in process.

Recommendations

  • To view the current amount of free (unallocated) space in the database. For more information, see Display Data and Log Space Information for a Database

  • Consider the following information when you plan to shrink a database:

    • A shrink operation is most effective after an operation that creates a large amount of unused storage space, such as a large DELETE statement, truncate table, or a drop table operation.

    • Most databases require some free space to be available for regular day-to-day operations. If you shrink a database repeatedly and notice that the database size grows again, this indicates that the free space is required for regular operations. In these cases, repeatedly shrinking the database is a wasted operation. Autogrow events necessary to grow the database file(s) hinder performance.

    • A shrink operation doesn't preserve the fragmentation state of indexes in the database, and generally increases fragmentation to a degree. This is another reason not to repeatedly shrink the database.

    • Unless you have a specific requirement, don't set the AUTO_SHRINK database option to ON.

Permissions

Requires membership in the sysadmin fixed server role or the db_owner fixed database role.

Remarks

Shrink operations in progress can block other queries on the database, and can be blocked by queries already in progress. Introduced in SQL Server 2022 (16.x), shrink database operations have a WAIT_AT_LOW_PRIORITY option. This feature is a new additional option for DBCC SHRINKDATABASE and DBCC SHRINKFILE. If a new shrink operation in WAIT_AT_LOW_PRIORITY mode can't obtain the necessary locks due to a long-running query already in progress, the shrink operation will eventually time out after one minute and silently exit, preventing other queries from being blocked. For more information, see DBCC SHRINKDATABASE.

For information on file management and shrink operations specific to Azure SQL Database, see Manage file space for databases in Azure SQL Database.

Use SQL Server Management Studio

Applies to: SQL Server, Azure SQL Managed Instance

Shrink a database

  1. In Object Explorer, connect to an instance of the SQL Server Database Engine, and then expand that instance.

  2. Expand Databases, and then right-click the database that you want to shrink.

  3. Point to Tasks, point to Shrink, and then select Database.

    • Database

      Displays the name of the selected database.

    • Current allocated space

      Displays the total used and unused space for the selected database.

    • Available free space

      Displays the sum of free space in the log and data files of the selected database.

    • Reorganize files before releasing unused space

      Selecting this option is equivalent to executing DBCC SHRINKDATABASE specifying a target percent option. Clearing this option is equivalent to executing DBCC SHRINKDATABASE with TRUNCATEONLY option. By default, this option isn't selected when the dialog is opened. If this option is selected, the user must specify a target percent option.

    • Maximum free space in files after shrinking

      Enter the maximum percentage of free space to be left in the database files after the database has been shrunk. Permissible values are between 0 and 99.

  4. Select OK.

Use Transact-SQL

Shrink a database

  1. Connect to the Database Engine.

  2. From the Standard bar, select New Query.

  3. Copy and paste the following example into the query window and select Execute. This example uses DBCC SHRINKDATABASE to decrease the size of the data and log files in the UserDB database, and to allow for 10 percent free space in the database.

DBCC SHRINKDATABASE (UserDB, 10);GO

After you shrink a database

Data that is moved to shrink a file can be scattered to any available location in the file. This causes index fragmentation and can slow the performance of queries that search a range of the index. To eliminate the fragmentation, consider rebuilding the indexes on the file after shrinking. For more information, see Rebuild an index.

Related content

  • Shrink the tempdb database
  • Considerations for the autogrow and autoshrink settings in SQL Server
  • Database Files and Filegroups
  • sys.databases (Transact-SQL)
  • sys.database_files (Transact-SQL)
  • DBCC SHRINKDATABASE (Transact-SQL)
  • DBCC SHRINKFILE (Transact-SQL)
  • Delete Data or Log Files from a Database
  • Shrink a file
  • Manage file space for databases in Azure SQL Database
Shrink a database - SQL Server (2024)

FAQs

What does shrink database do in SQL Server? ›

By using the Shrink Database task, a package can shrink files for a single database or multiple databases. Shrinking data files recovers space by moving pages of data from the end of the file to unoccupied space closer to the front of the file.

How do I reduce the size of a SQL Server database? ›

Shrink a database
  1. In Object Explorer, connect to an instance of the SQL Server Database Engine, and then expand that instance.
  2. Expand Databases, and then right-click the database that you want to shrink.
  3. Point to Tasks, point to Shrink, and then select Database. Database. ...
  4. Select OK.
Nov 2, 2023

How to shrink database in SQL Server command line? ›

To shrink all data and log files for a specific database, execute the DBCC SHRINKDATABASE command. To shrink one data or log file at a time for a specific database, execute the DBCC SHRINKFILE command. To view the current amount of free (unallocated) space in the database, run sp_spaceused.

How do I compress a SQL Server database? ›

To change the compression settings for both tables and indexes using the Data Compression wizard, perform the following steps:
  1. Start SQL Server Management Studio.
  2. Right-click the table or index you want to compress, click Storage, and click Manage Compression. ...
  3. Click Next. ...
  4. Choose the type of compression.

What are the disadvantages of shrinking database in SQL Server? ›

But shrinking may affect your database performance significantly, if not properly done. Shrinking will increase fragmentation and will cause any DB operation costly. Rebuild indexes is necessary after DB shrink to reduce fragmentation and increase performance.

Should I shrink my database? ›

Using On-premises database it is usually a bad idea to shrink the log file since it can impact performance, but using Azure SQL Database it even make less sense in most cases, since Azure SQL Database automatically shrinks transaction log file to avoid excessive space usage.

How do I reduce the size of my database? ›

Reduction of the database size
  1. Store the low-level data for the shortest possible time. ...
  2. Store less low-level data. ...
  3. Use Event Filters to selectively not store low-level events from some executables or computers. ...
  4. Check database settings that can cause increased disk usage:

How do I reduce the size of a database? ›

6 Easy Tips to reduce the size of MySQL Database
  1. Backup, first but not least.
  2. List MySQL Table and Index Size.
  3. Delete Unwanted Data.
  4. Find and Remove Unused Indexes.
  5. Shrink and Optimize MySQL.
  6. Optimize Datatypes for Columns.
  7. Enable Columns Compression (Only InnoDB)
  8. Compress Table (Only MyISAM)

How do I manage the size of a SQL database? ›

Using SQL Server Management Studio

Expand Databases, right-click the database to increase, and then click Properties. In Database Properties, select the Files page. To increase the size of an existing file, increase the value in the Initial Size (MB) column for the file.

How do I shrink a SQL database and log file? ›

Right-click the database again and select Tasks -> Shrink -> Files. Change the type to Log . Under Shrink action, select Reorganize pages before releasing unused space and click OK. When the process completes, switch the recovery model back to Full or Bulk-Logged and take a full database backup.

What is the difference between shrink database and shrink file? ›

You can choose between the DBCC SHRINKDATABASE command and the DBCC SHRINKFILE command. The former tries to shrink all data and log files, while the latter shrinks a specific file of your choice. There is no reason to shrink all files at once.

What is the difference between shrink and compress in SQL Server? ›

SQL DBA,SQL Server MVP(07, 08, 09) A socialist is someone who will give you the shirt off *someone else's* back. Scott has a good idea. Compression reads the data and then rewrites some, so that's a load. Shrink just moves pages around from the end of the file to the middle, and then lessens the allocation from the OS.

How do I compress all tables in SQL Server? ›

Compress Tables:
  1. --Creates the ALTER TABLE Statements.
  2. SET NOCOUNT ON.
  3. SELECT.
  4. 'ALTER TABLE ['
  5. + s.[name]
  6. + '].['
  7. + o.[name]
  8. + '] REBUILD WITH (DATA_COMPRESSION=PAGE);'

What does shrink a database do why would it not be a good idea? ›

When you shrink a database, you are asking SQL Server to remove the unused space from your database's files. The process SQL uses can be ugly and result in Index fragmentation. This fragmentation affects performance in the long run.

What are the disadvantages of shrinking database? ›

Shrinking a SQL database is generally not recommended as a regular maintenance task. While it can free up disk space, shrinking can lead to performance issues, fragmentation, and increased resource consumption during subsequent growth.

What is the difference between truncate and shrink in SQL? ›

While truncating the log makes the space available for use again, it does not decrease the transaction log file size. To reduce the size of the transaction log file, you need to shrink the log file.

Top Articles
Latest Posts
Article information

Author: Terence Hammes MD

Last Updated:

Views: 5274

Rating: 4.9 / 5 (69 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Terence Hammes MD

Birthday: 1992-04-11

Address: Suite 408 9446 Mercy Mews, West Roxie, CT 04904

Phone: +50312511349175

Job: Product Consulting Liaison

Hobby: Jogging, Motor sports, Nordic skating, Jigsaw puzzles, Bird watching, Nordic skating, Sculpting

Introduction: My name is Terence Hammes MD, I am a inexpensive, energetic, jolly, faithful, cheerful, proud, rich person who loves writing and wants to share my knowledge and understanding with you.