< Back
Remove-DbaDatabaseSafely
Post
NAME Remove-DbaDatabaseSafely
SYNOPSIS
Safely removes a SQL Database and creates an Agent Job to restore it.
SYNTAX
Remove-DbaDatabaseSafely [-SqlInstance] <DbaInstanceParameter> [[-SqlCredential] <Pscredential>] [[-Database]
<System.Object[]>] [[-Destination] <DbaInstanceParameter>] [[-DestinationCredential] <Pscredential>]
[-NoDbccCheckDb <Switch>] [-BackupFolder] <String> [[-CategoryName] <String>] [[-JobOwner] <String>]
[-AllDatabases <Switch>] [[-BackupCompression] <String>] [-ReuseSourceFolderStructure <Switch>] [-Force <Switch>]
[-EnableException <Switch>] [<CommonParameters>]
DESCRIPTION
Performs a DBCC CHECKDB on the database, backs up the database with Checksum and verify only to a final (golden)
backup location, creates an Agent Job to restore from that backup, drops the database, runs the agent job to
restore the database, performs a DBCC CHECKDB and drops the database.
With huge thanks to Grant Fritchey and his verify your backups video. Take a look, it's only 3 minutes long.
http://sqlps.io/backuprant
PARAMETERS
-AllDatabases [<Switch>]
If this switch is enabled, all user databases on the server will be removed. This is useful when
decommissioning a server. You should use a Destination with this switch.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-BackupCompression [<String>]
If this switch is enabled, compression will be used for the backup regardless of the SQL Server instance
setting. By default, the SQL Server instance setting for backup compression is used.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-BackupFolder [<String>]
Specifies the path to a folder where the final backups of the removed databases will be stored. If you are
using separate source and destination servers, you must specify a UNC path such as \\\\SERVER1\\BACKUPSHARE\\
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-CategoryName [<String>]
Specifies the Category Name for the Agent job that is created for restoring the database(s). By default, the
name is "Rationalisation".
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-Database [<System.Object[]>]
Specifies one or more databases to remove.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-Destination [<DbaInstanceParameter>]
If specified, Agent jobs will be created on this server. By default, the jobs will be created on the server
specified by SqlInstance. You must have sysadmin access and the server must be SQL Server 2000 or higher. The
SQL Agent service will be started if it is not already running.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-DestinationCredential [<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
-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
-Force [<Switch>]
If this switch is enabled, all actions will be performed even if DBCC errors are detected. An Agent job will
be created with 'DBCCERROR' in the name and the backup file will have 'DBCC' in its name.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-JobOwner [<String>]
Specifies the name of the account which will own the Agent jobs. By default, sa is used.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-NoDbccCheckDb [<Switch>]
If this switch is enabled, the initial DBCC CHECK DB will be skipped. This will make the process quicker but
will also allow you to create an Agent job that restores a database backup containing a corrupt database.
A second DBCC CHECKDB is performed on the restored database so you will still be notified BUT USE THIS WITH
CARE.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-ReuseSourceFolderStructure [<Switch>]
If this switch is enabled, the source folder structure will be used when restoring instead of using the
destination instance default folder structure.
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 [<DbaInstanceParameter>]
The target SQL Server instance or instances. You must have sysadmin access and server version must be SQL
Server version 2000 or higher.
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: Database, Remove
Author: Rob Sewell (@SQLDBAWithBeard), sqldbawithabeard.com
Website: https://dbatools.io
Copyright: (c) 2018 by dbatools, licensed under MIT
License: MIT https://opensource.org/licenses/MIT
-------------------------- EXAMPLE 1 --------------------------
PS C:\\>Remove-DbaDatabaseSafely -SqlInstance 'Fade2Black' -Database RideTheLightning -BackupFolder
'C:\\MSSQL\\Backup\\Rationalised - DO NOT DELETE'
Performs a DBCC CHECKDB on database RideTheLightning on server Fade2Black. If there are no errors, the database is
backup to the folder C:\\MSSQL\\Backup\\Rationalised - DO NOT DELETE. Then, an Agent job to restore the database from
that backup is created. The database is then dropped, the Agent job to restore it run, a DBCC CHECKDB run against
the restored database, and then it is dropped again.
Any DBCC errors will be written to your documents folder
-------------------------- EXAMPLE 2 --------------------------
PS C:\\>$Database = 'DemoNCIndex','RemoveTestDatabase'
PS C:\\> Remove-DbaDatabaseSafely -SqlInstance 'Fade2Black' -Database $Database -BackupFolder
'C:\\MSSQL\\Backup\\Rationalised - DO NOT DELETE'
Performs a DBCC CHECKDB on two databases, 'DemoNCIndex' and 'RemoveTestDatabase' on server Fade2Black. Then, an
Agent job to restore each database from those backups is created. The databases are then dropped, the Agent jobs
to restore them run, a DBCC CHECKDB run against the restored databases, and then they are dropped again.
Any DBCC errors will be written to your documents folder
-------------------------- EXAMPLE 3 --------------------------
PS C:\\>Remove-DbaDatabaseSafely -SqlInstance 'Fade2Black' -Destination JusticeForAll -Database RideTheLightning
-BackupFolder '\\\\BACKUPSERVER\\BACKUPSHARE\\MSSQL\\Rationalised - DO NOT DELETE'
Performs a DBCC CHECKDB on database RideTheLightning on server Fade2Black. If there are no errors, the database is
backup to the folder \\\\BACKUPSERVER\\BACKUPSHARE\\MSSQL\\Rationalised - DO NOT DELETE . Then, an Agent job is created
on server JusticeForAll to restore the database from that backup is created. The database is then dropped on
Fade2Black, the Agent job to restore it on JusticeForAll is run, a DBCC CHECKDB run against the restored database,
and then it is dropped from JusticeForAll.
Any DBCC errors will be written to your documents folder
-------------------------- EXAMPLE 4 --------------------------
PS C:\\>Remove-DbaDatabaseSafely -SqlInstance IronMaiden -Database $Database -Destination TheWildHearts
-BackupFolder Z:\\Backups -NoDbccCheckDb -JobOwner 'THEBEARD\\Rob'
For the databases $Database on the server IronMaiden a DBCC CHECKDB will not be performed before backing up the
databases to the folder Z:\\Backups. Then, an Agent job is created on server TheWildHearts with a Job Owner of
THEBEARD\\Rob to restore each database from that backup using the instance's default file paths. The database(s)
is(are) then dropped on IronMaiden, the Agent job(s) run, a DBCC CHECKDB run on the restored database(s), and then
the database(s) is(are) dropped.
-------------------------- EXAMPLE 5 --------------------------
PS C:\\>Remove-DbaDatabaseSafely -SqlInstance IronMaiden -Database $Database -Destination TheWildHearts
-BackupFolder Z:\\Backups
The databases $Database on the server IronMaiden will be backed up the to the folder Z:\\Backups. Then, an Agent
job is created on server TheWildHearts with a Job Owner of THEBEARD\\Rob to restore each database from that backup
using the instance's default file paths. The database(s) is(are) then dropped on IronMaiden, the Agent job(s) run,
a DBCC CHECKDB run on the restored database(s), and then the database(s) is(are) dropped.
If there is a DBCC Error, the function will continue to perform rest of the actions and will create an Agent job
with 'DBCCERROR' in the name and a Backup file with 'DBCCError' in the name.
RELATED LINKS
https://dbatools.io/Remove-DbaDatabaseSafely
SYNOPSIS
Safely removes a SQL Database and creates an Agent Job to restore it.
SYNTAX
Remove-DbaDatabaseSafely [-SqlInstance] <DbaInstanceParameter> [[-SqlCredential] <Pscredential>] [[-Database]
<System.Object[]>] [[-Destination] <DbaInstanceParameter>] [[-DestinationCredential] <Pscredential>]
[-NoDbccCheckDb <Switch>] [-BackupFolder] <String> [[-CategoryName] <String>] [[-JobOwner] <String>]
[-AllDatabases <Switch>] [[-BackupCompression] <String>] [-ReuseSourceFolderStructure <Switch>] [-Force <Switch>]
[-EnableException <Switch>] [<CommonParameters>]
DESCRIPTION
Performs a DBCC CHECKDB on the database, backs up the database with Checksum and verify only to a final (golden)
backup location, creates an Agent Job to restore from that backup, drops the database, runs the agent job to
restore the database, performs a DBCC CHECKDB and drops the database.
With huge thanks to Grant Fritchey and his verify your backups video. Take a look, it's only 3 minutes long.
http://sqlps.io/backuprant
PARAMETERS
-AllDatabases [<Switch>]
If this switch is enabled, all user databases on the server will be removed. This is useful when
decommissioning a server. You should use a Destination with this switch.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-BackupCompression [<String>]
If this switch is enabled, compression will be used for the backup regardless of the SQL Server instance
setting. By default, the SQL Server instance setting for backup compression is used.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-BackupFolder [<String>]
Specifies the path to a folder where the final backups of the removed databases will be stored. If you are
using separate source and destination servers, you must specify a UNC path such as \\\\SERVER1\\BACKUPSHARE\\
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-CategoryName [<String>]
Specifies the Category Name for the Agent job that is created for restoring the database(s). By default, the
name is "Rationalisation".
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-Database [<System.Object[]>]
Specifies one or more databases to remove.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-Destination [<DbaInstanceParameter>]
If specified, Agent jobs will be created on this server. By default, the jobs will be created on the server
specified by SqlInstance. You must have sysadmin access and the server must be SQL Server 2000 or higher. The
SQL Agent service will be started if it is not already running.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-DestinationCredential [<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
-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
-Force [<Switch>]
If this switch is enabled, all actions will be performed even if DBCC errors are detected. An Agent job will
be created with 'DBCCERROR' in the name and the backup file will have 'DBCC' in its name.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-JobOwner [<String>]
Specifies the name of the account which will own the Agent jobs. By default, sa is used.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-NoDbccCheckDb [<Switch>]
If this switch is enabled, the initial DBCC CHECK DB will be skipped. This will make the process quicker but
will also allow you to create an Agent job that restores a database backup containing a corrupt database.
A second DBCC CHECKDB is performed on the restored database so you will still be notified BUT USE THIS WITH
CARE.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-ReuseSourceFolderStructure [<Switch>]
If this switch is enabled, the source folder structure will be used when restoring instead of using the
destination instance default folder structure.
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 [<DbaInstanceParameter>]
The target SQL Server instance or instances. You must have sysadmin access and server version must be SQL
Server version 2000 or higher.
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: Database, Remove
Author: Rob Sewell (@SQLDBAWithBeard), sqldbawithabeard.com
Website: https://dbatools.io
Copyright: (c) 2018 by dbatools, licensed under MIT
License: MIT https://opensource.org/licenses/MIT
-------------------------- EXAMPLE 1 --------------------------
PS C:\\>Remove-DbaDatabaseSafely -SqlInstance 'Fade2Black' -Database RideTheLightning -BackupFolder
'C:\\MSSQL\\Backup\\Rationalised - DO NOT DELETE'
Performs a DBCC CHECKDB on database RideTheLightning on server Fade2Black. If there are no errors, the database is
backup to the folder C:\\MSSQL\\Backup\\Rationalised - DO NOT DELETE. Then, an Agent job to restore the database from
that backup is created. The database is then dropped, the Agent job to restore it run, a DBCC CHECKDB run against
the restored database, and then it is dropped again.
Any DBCC errors will be written to your documents folder
-------------------------- EXAMPLE 2 --------------------------
PS C:\\>$Database = 'DemoNCIndex','RemoveTestDatabase'
PS C:\\> Remove-DbaDatabaseSafely -SqlInstance 'Fade2Black' -Database $Database -BackupFolder
'C:\\MSSQL\\Backup\\Rationalised - DO NOT DELETE'
Performs a DBCC CHECKDB on two databases, 'DemoNCIndex' and 'RemoveTestDatabase' on server Fade2Black. Then, an
Agent job to restore each database from those backups is created. The databases are then dropped, the Agent jobs
to restore them run, a DBCC CHECKDB run against the restored databases, and then they are dropped again.
Any DBCC errors will be written to your documents folder
-------------------------- EXAMPLE 3 --------------------------
PS C:\\>Remove-DbaDatabaseSafely -SqlInstance 'Fade2Black' -Destination JusticeForAll -Database RideTheLightning
-BackupFolder '\\\\BACKUPSERVER\\BACKUPSHARE\\MSSQL\\Rationalised - DO NOT DELETE'
Performs a DBCC CHECKDB on database RideTheLightning on server Fade2Black. If there are no errors, the database is
backup to the folder \\\\BACKUPSERVER\\BACKUPSHARE\\MSSQL\\Rationalised - DO NOT DELETE . Then, an Agent job is created
on server JusticeForAll to restore the database from that backup is created. The database is then dropped on
Fade2Black, the Agent job to restore it on JusticeForAll is run, a DBCC CHECKDB run against the restored database,
and then it is dropped from JusticeForAll.
Any DBCC errors will be written to your documents folder
-------------------------- EXAMPLE 4 --------------------------
PS C:\\>Remove-DbaDatabaseSafely -SqlInstance IronMaiden -Database $Database -Destination TheWildHearts
-BackupFolder Z:\\Backups -NoDbccCheckDb -JobOwner 'THEBEARD\\Rob'
For the databases $Database on the server IronMaiden a DBCC CHECKDB will not be performed before backing up the
databases to the folder Z:\\Backups. Then, an Agent job is created on server TheWildHearts with a Job Owner of
THEBEARD\\Rob to restore each database from that backup using the instance's default file paths. The database(s)
is(are) then dropped on IronMaiden, the Agent job(s) run, a DBCC CHECKDB run on the restored database(s), and then
the database(s) is(are) dropped.
-------------------------- EXAMPLE 5 --------------------------
PS C:\\>Remove-DbaDatabaseSafely -SqlInstance IronMaiden -Database $Database -Destination TheWildHearts
-BackupFolder Z:\\Backups
The databases $Database on the server IronMaiden will be backed up the to the folder Z:\\Backups. Then, an Agent
job is created on server TheWildHearts with a Job Owner of THEBEARD\\Rob to restore each database from that backup
using the instance's default file paths. The database(s) is(are) then dropped on IronMaiden, the Agent job(s) run,
a DBCC CHECKDB run on the restored database(s), and then the database(s) is(are) dropped.
If there is a DBCC Error, the function will continue to perform rest of the actions and will create an Agent job
with 'DBCCERROR' in the name and a Backup file with 'DBCCError' in the name.
RELATED LINKS
https://dbatools.io/Remove-DbaDatabaseSafely