How Do I Remove CDC Objects After I Stop Using a SQL Server Binary Log Reader Connection?
Question
How do I remove CDC objects after I stop using a Fivetran SQL Server Binary Log Reader connection?
Environment
- Connector: SQL Server
- Incremental sync method: Binary Log Reader
Answer
When you stop using your SQL Server Binary Log Reader connection, you should run the Binary Log Reader uninstall script to remove the stored procedures, permissions, and related objects that Fivetran created during setup. By default, the script leaves CDC objects in place. To remove them, use the -DisableCdc switch or disable CDC manually, as described below.
Running the uninstall script is especially important if your connection used the default ivetran log truncation mode. In this mode, Fivetran calls sp_repldone to advance the SQL Server truncation point. If the connection stops running and no other process calls sp_repldone, the truncation point stops advancing, which can cause transaction log bloat. For more information, see SQL Server Binary Log Reader FAQs.
Prepare for cleanup
Pause or delete the connection to prevent sync failures when the script removes the objects:
i. In Fivetran, go to your SQL Server connection page.
ii. In the top right corner, set the Enabled toggle to OFF.
Use a SQL Server account with permission to run the commands in the script. To disable CDC manually, the account must belong to the
sysadminserver role for database-level changes or thedb_ownerdatabase role for table-level changes.
Run the uninstall script
In Fivetran, go to Account Settings > Downloads.
Download the Binary Log Reader uninstall script for your operating system.
Run the script:
.\BinLogUninstall-<version>.ps1 -SqlInstance <instance> -SourceDatabase <database>
The script accepts the following parameters:
(Required)
-SqlInstance: The SQL Server instance name, such aslocalhost\SQLEXPRESS.(Required)
-SourceDatabase: The database from which to remove the stored procedures.(Optional)
-ClrDatabaseand-NoDll: Use these parameters together if you installed Binary Log Reader with a shared CLR database. The script removes only the wrapper procedures and database-level permissions in the source database. It doesn't remove the shared CLR assembly, CLR procedures,FivetranUserlogin, trusted assembly,msdbhelper procedures, or the minimal-permissions user in the CLR database andmsdb, because other source databases might use them.-ClrDatabaseis required with-NoDll.If you also pass
-MinimalPermissionsUser, the script still revokes that login's server-level permissions (VIEW SERVER STATEorVIEW SERVER PERFORMANCE STATE, andADMINISTER BULK OPERATIONS), even with-NoDll. Other connections that share the login lose these permissions. Don't pass-MinimalPermissionsUserwhile other connections still use the login.(Optional)
-SqlAdminUserand-SqlPassword: The SQL admin credentials. If you don't provide them, the script uses Windows Authentication. If you provide-SqlAdminUser, you must also provide-SqlPassword.(Optional)
-MinimalPermissionsUser: Use this parameter if you created a dedicated SQL Server login with minimal permissions for Fivetran. The script removes that login's database users and server-level grants. It doesn't drop the login itself, so you must drop the login manually if you no longer need it.(Optional)
-UseDBDatareaderRole: Use this parameter if you added the minimal-permissions user to thedb_datareaderrole. If you don't set it, the script also removes thefivetran_datareaderrole if it has no remaining members.(Optional)
-DisableCdc: Disables the CDC capture instances Fivetran created, releases the transaction log truncation point, and, if no other capture instances remain, disables CDC on the source database. For more information, see Remove CDC objects.
The helper procedures we create in msdb, and the msdb user for the minimal-permissions login, are shared by every source database on the instance. The script removes them only when no other database on the instance still has procedures we created. Otherwise, it leaves them in place and lists the databases that still use them. It treats offline or unreadable databases as still in use. With -NoDll, it never removes these shared objects. The FivetranUser login, the FivetranCLRKey key, and the trusted assembly entries in master follow the same rule: the script removes them only when no other database still has the Fivetran CLR assembly. The Linux script also applies the shared-use check when removing the BulkOpsCert certificates and certificate login. The PowerShell script doesn't remove these certificate objects.
To see all available parameters, run the script with the -Help switch.
Remove CDC objects
Disabling CDC at the database level drops all CDC system objects, including change tables, jobs, stored procedures, and functions. Disable CDC only if no other tools or processes depend on it for this database.
Choose one of the following options:
Run the uninstall script with the
-DisableCdcswitch to remove the setup objects and CDC objects in the same run:.\BinLogUninstall-<version>.ps1 -SqlInstance <instance> -SourceDatabase <database> -DisableCdcThe script performs the following steps:
- Disables each capture instance that Fivetran created (named
fivetran_<object_id>), one table at a time. If a table is blocked by an open transaction or session, the script waits up to 30 seconds and retries up to three times before moving on. - Releases the transaction log truncation point that Binary Log Reader was advancing and closes the log reader session, so the transaction log can truncate again. The script skips this step if capture instances that are still in use remain, or if it couldn't disable one of the Fivetran capture instances, because releasing the truncation point would mark their pending changes as captured. It also skips this step if another session is already reading the log, such as the CDC capture job or a replication Log Reader Agent, because that reader owns the truncation point.
- Disables CDC at the database level only if no capture instances that are still in use remain. If capture instances that Fivetran didn't create are still in use, the script leaves CDC enabled and reports how many remain. Orphaned capture instances, whose source table was dropped, don't keep CDC enabled. They can't be disabled individually, so they're removed only when CDC is disabled at the database level.
- Disables each capture instance that Fivetran created (named
After running the uninstall script, disable CDC manually in SQL Server:
USE <database> GO EXEC sys.sp_cdc_disable_db GO
If other tools or processes still depend on CDC, disable only the capture instances associated with Fivetran. A table can have multiple capture instances. Use @capture_instance = N'all' only if you're certain that no other tool or process uses any of the table's capture instances.
To disable the Fivetran capture instance for the table:
List the capture instances for the table:
USE <database> GO EXEC sys.sp_cdc_help_change_data_capture @source_schema = N'<schema>', @source_name = N'<table>' GOIdentify the capture instance associated with Fivetran. Fivetran-created capture instances are often named
fivetran_<object_id>, where<object_id>is the source table's object ID. Always use the name returned by SQL Server. If you dropped and recreated the table after setup, its object ID changes, and the capture instance name may not match the current object ID.If you aren't sure which capture instance Fivetran created, don't disable any of them.
Disable the Fivetran capture instance. Replace
<capture_instance>with the capture instance name you identified in step two:EXEC sys.sp_cdc_disable_table @source_schema = N'<schema>', @source_name = N'<table>', @capture_instance = N'<capture_instance>' GO
For more information, see the Microsoft sys.sp_cdc_disable_table documentation.