< Back
Invoke-DbaDbShrink
Post
NAME Invoke-DbaDbShrink
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
SYNTAX
Invoke-DbaDbShrink [-SqlInstance] <Sqlcollaborative.Dbatools.Parameter.DbaInstanceParameter[]> [[-SqlCredential]
<Pscredential>] [[-Database] <System.Object[]>] [[-ExcludeDatabase] <System.Object[]>] [-AllUserDatabases
<Switch>] [[-PercentFreeSpace] <Int>] [[-ShrinkMethod] <String>] [[-FileType] <String>] [[-StepSize] <Long>]
[[-StatementTimeout] <Int>] [-ExcludeIndexStats <Switch>] [-ExcludeUpdateUsage <Switch>] [-EnableException
<Switch>] [<CommonParameters>]
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 ... data-files
http://sqlmag.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.
PARAMETERS
-AllUserDatabases [<Switch>]
Run command against all user databases.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-Database [<System.Object[]>]
The database(s) to process - this list is auto-populated from the server. If unspecified, all databases will
be processed.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-EnableException [<Switch>]
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.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-ExcludeDatabase [<System.Object[]>]
The database(s) to exclude - this list is auto-populated from the server.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-ExcludeIndexStats [<Switch>]
Exclude statistics about fragmentation.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-ExcludeUpdateUsage [<Switch>]
Exclude DBCC UPDATE USAGE for database.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-FileType [<String>]
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
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-PercentFreeSpace [<Int>]
Specifies how much free space to leave, defaults to 0.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-ShrinkMethod [<String>]
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.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-SqlCredential [<Pscredential>]
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..
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-SqlInstance [<Sqlcollaborative.Dbatools.Parameter.DbaInstanceParameter[]>]
The target SQL Server instance or instances. Defaults to the default instance on localhost.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-StatementTimeout [<Int>]
Timeout in minutes. Defaults to infinity (shrinks can take a while).
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-StepSize [<Long>]
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.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
<CommonParameters>
This cmdlet supports the common parameters: Verbose, Debug,
ErrorAction, ErrorVariable, WarningAction, WarningVariable,
OutBuffer, PipelineVariable, and OutVariable. For more information, see
about_CommonParameters (https:/go.microsoft.com/fwlink/?LinkID=113216).
INPUTS
OUTPUTS
NOTES
Tags: Shrink, Database
Author: Chrissy LeMaire (@cl), netnerds.net
Website: https://dbatools.io
Copyright: (c) 2018 by dbatools, licensed under MIT
License: MIT https://opensource.org/licenses/MIT
-------------------------- 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)
RELATED LINKS
https://dbatools.io/Invoke-DbaDbShrink
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
SYNTAX
Invoke-DbaDbShrink [-SqlInstance] <Sqlcollaborative.Dbatools.Parameter.DbaInstanceParameter[]> [[-SqlCredential]
<Pscredential>] [[-Database] <System.Object[]>] [[-ExcludeDatabase] <System.Object[]>] [-AllUserDatabases
<Switch>] [[-PercentFreeSpace] <Int>] [[-ShrinkMethod] <String>] [[-FileType] <String>] [[-StepSize] <Long>]
[[-StatementTimeout] <Int>] [-ExcludeIndexStats <Switch>] [-ExcludeUpdateUsage <Switch>] [-EnableException
<Switch>] [<CommonParameters>]
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 ... data-files
http://sqlmag.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.
PARAMETERS
-AllUserDatabases [<Switch>]
Run command against all user databases.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-Database [<System.Object[]>]
The database(s) to process - this list is auto-populated from the server. If unspecified, all databases will
be processed.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-EnableException [<Switch>]
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.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-ExcludeDatabase [<System.Object[]>]
The database(s) to exclude - this list is auto-populated from the server.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-ExcludeIndexStats [<Switch>]
Exclude statistics about fragmentation.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-ExcludeUpdateUsage [<Switch>]
Exclude DBCC UPDATE USAGE for database.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-FileType [<String>]
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
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-PercentFreeSpace [<Int>]
Specifies how much free space to leave, defaults to 0.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-ShrinkMethod [<String>]
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.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-SqlCredential [<Pscredential>]
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..
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-SqlInstance [<Sqlcollaborative.Dbatools.Parameter.DbaInstanceParameter[]>]
The target SQL Server instance or instances. Defaults to the default instance on localhost.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-StatementTimeout [<Int>]
Timeout in minutes. Defaults to infinity (shrinks can take a while).
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-StepSize [<Long>]
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.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
<CommonParameters>
This cmdlet supports the common parameters: Verbose, Debug,
ErrorAction, ErrorVariable, WarningAction, WarningVariable,
OutBuffer, PipelineVariable, and OutVariable. For more information, see
about_CommonParameters (https:/go.microsoft.com/fwlink/?LinkID=113216).
INPUTS
OUTPUTS
NOTES
Tags: Shrink, Database
Author: Chrissy LeMaire (@cl), netnerds.net
Website: https://dbatools.io
Copyright: (c) 2018 by dbatools, licensed under MIT
License: MIT https://opensource.org/licenses/MIT
-------------------------- 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)
RELATED LINKS
https://dbatools.io/Invoke-DbaDbShrink