commands

^

Invoke-DbaDbShrink

Author Chrissy LeMaire (@cl), netnerds.net
Availability Windows, Linux, macOS

 

Synopsis

Shrinks all files in a database. This is a command that should rarely be used.

  • Shrinks can cause severe index fragmentation (to the tune of 99%)
  • Shrinks can cause massive growth in the database's transaction log
  • Shrinks can require a lot of time and system resources to perform data movement

Description

Shrinks all files in a database. Databases should be shrunk only when completely necessary.

Many awesome SQL people have written about why you should not shrink your data files. Paul Randal and Kalen Delaney wrote great posts about this topic:

http://www.sqlskills.com/blogs/paul/why-you-should-not-shrink-your-data-files https://www.itprotoday.com/sql-server/shrinking-data-files

However, there are some cases where a database will need to be shrunk. In the event that you must shrink your database:

  1. Ensure you have plenty of space for your T-Log to grow
  2. Understand that shrinks require a lot of CPU and disk resources
  3. Consider running DBCC INDEXDEFRAG or ALTER INDEX ... REORGANIZE after the shrink is complete.

Syntax

Invoke-DbaDbShrink
    [-SqlInstance] <DbaInstanceParameter[]>
    [[-SqlCredential] <PSCredential>]
    [[-Database] <Object[]>]
    [[-ExcludeDatabase] <Object[]>]
    [-AllUserDatabases]
    [[-PercentFreeSpace] <Int32>]
    [[-ShrinkMethod] <String>]
    [[-FileType] <String>]
    [[-StepSize] <Int64>]
    [[-StatementTimeout] <Int32>]
    [-ExcludeIndexStats]
    [-ExcludeUpdateUsage]
    [-EnableException]
    [-WhatIf]
    [-Confirm]
    [<CommonParameters>]

 

Examples

 

Example: 1
PS C:\> Invoke-DbaDbShrink -SqlInstance sql2016 -Database Northwind,pubs,Adventureworks2014

Shrinks Northwind, pubs and Adventureworks2014 to have as little free space as possible.

Example: 2
PS C:\> Invoke-DbaDbShrink -SqlInstance sql2014 -Database AdventureWorks2014 -PercentFreeSpace 50

Shrinks AdventureWorks2014 to have 50% free space. So let's say AdventureWorks2014 was 1GB and it's using 100MB space. The database free space would be reduced to 50MB.

Example: 3
PS C:\> Invoke-DbaDbShrink -SqlInstance sql2014 -Database AdventureWorks2014 -PercentFreeSpace 50 -FileType Data -StepSize 25MB

Shrinks AdventureWorks2014 to have 50% free space, runs shrinks in 25MB chunks for improved performance.

Example: 4
PS C:\> Invoke-DbaDbShrink -SqlInstance sql2012 -AllUserDatabases

Shrinks all user databases on SQL2012 (not ideal for production)

Required Parameters

-SqlInstance

The target SQL Server instance or instances. Defaults to the default instance on localhost.

Alias
Required True
Pipeline true (ByValue)
Default Value

Optional Parameters

-SqlCredential

Login to the target instance using alternative credentials. Accepts PowerShell credentials (Get-Credential). Windows Authentication, SQL Server Authentication, Active Directory - Password, and Active Directory - Integrated are all supported. For MFA support, please use Connect-DbaInstance..

Alias
Required False
Pipeline false
Default Value
-Database

The database(s) to process - this list is auto-populated from the server. If unspecified, all databases will be processed.

Alias
Required False
Pipeline false
Default Value
-ExcludeDatabase

The database(s) to exclude - this list is auto-populated from the server.

Alias
Required False
Pipeline false
Default Value
-AllUserDatabases

Run command against all user databases.

Alias
Required False
Pipeline false
Default Value False
-PercentFreeSpace

Specifies how much free space to leave, defaults to 0.

Alias
Required False
Pipeline false
Default Value 0
-ShrinkMethod

Specifies the method that is used to shrink the database Default Data in pages located at the end of a file is moved to pages earlier in the file. Files are truncated to reflect allocated space. EmptyFile Migrates all of the data from the referenced file to other files in the same filegroup. (DataFile and LogFile objects only). NoTruncate Data in pages located at the end of a file is moved to pages earlier in the file. TruncateOnly Data distribution is not affected. Files are truncated to reflect allocated space, recovering free space at the end of any file.

Alias
Required False
Pipeline false
Default Value Default
Accepted Values Default,EmptyFile,NoTruncate,TruncateOnly
-FileType

Specifies the files types that will be shrunk All - All Data and Log files are shrunk, using database shrink (Default) Data - Just the Data files are shrunk using file shrink Log - Just the Log files are shrunk using file shrink

Alias
Required False
Pipeline false
Default Value All
Accepted Values All,Data,Log
-StepSize

Measured in bits - but no worries! PowerShell has a very cool way of formatting bits. Just specify something like: 1MB or 10GB. See the examples for more information. If specified, this will chunk a larger shrink operation into multiple smaller shrinks. If shrinking a file by a large amount there are benefits of doing multiple smaller chunks.

Alias
Required False
Pipeline false
Default Value 0
-StatementTimeout

Timeout in minutes. Defaults to infinity (shrinks can take a while).

Alias
Required False
Pipeline false
Default Value 0
-ExcludeIndexStats

Exclude statistics about fragmentation.

Alias
Required False
Pipeline false
Default Value False
-ExcludeUpdateUsage

Exclude DBCC UPDATE USAGE for database.

Alias
Required False
Pipeline false
Default Value False
-EnableException

By default, when something goes wrong we try to catch it, interpret it and give you a friendly warning message. This avoids overwhelming you with "sea of red" exceptions, but is inconvenient because it basically disables advanced scripting. Using this switch turns this "nice by default" feature off and enables you to catch exceptions with your own try/catch.

Alias
Required False
Pipeline false
Default Value False
-WhatIf

Shows what would happen if the command were to run.

Alias wi
Required False
Pipeline false
Default Value
-Confirm

Prompts for confirmation of every step. For example: Are you sure you want to perform this action? Performing the operation "Shrink database" on target "pubs on SQL2016\VNEXT". [Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"):

Alias cf
Required False
Pipeline false
Default Value

 

Want to see the source code for this command? Check out Invoke-DbaDbShrink on GitHub.
Want to see the Bill Of Health for this command? Check out Invoke-DbaDbShrink.