Skip to content

Latest commit

 

History

History
129 lines (93 loc) · 6.69 KB

delete-an-article.md

File metadata and controls

129 lines (93 loc) · 6.69 KB
title description author ms.author ms.date ms.service ms.subservice ms.topic ms.custom helpviewer_keywords dev_langs monikerRange
Delete an Article
Delete an Article
MashaMSFT
mathoma
09/25/2024
sql
replication
how-to
updatefrequency5
articles [SQL Server replication], dropping
sp_droparticle
sp_dropmergearticle
deleting articles
removing articles
dropping articles
TSQL
=azuresqldb-mi-current||>=sql-server-2016

Delete an Article

[!INCLUDEsql-asdbmi] This topic describes how to delete an article in [!INCLUDEssnoversion] by using [!INCLUDEtsql] or Replication Management Objects (RMO). For information about the conditions under which articles can be dropped and whether dropping an article requires a new snapshot or the reinitialization of subscriptions, see Add Articles to and Drop Articles from Existing Publications.

In This Topic

Using Transact-SQL

Articles can be deleted programmatically using replication stored procedures. The stored procedures that you use depend on the type of publication to which the article belongs.

To delete an article from a snapshot or transactional publication

  1. Execute sp_droparticle (Transact-SQL) to delete an article, specified by @article, from a publication, specified by @publication. Specify a value of 1 for @force_invalidate_snapshot.

  2. (Optional) To remove the published object from the database entirely, execute the DROP <objectname> command at the Publisher on the publication database.

To delete an article from a merge publication

  1. Execute sp_dropmergearticle (Transact-SQL) to delete an article, specified by @article, from a publication, specified by @publication. If necessary, specify a value of 1 for @force_invalidate_snapshot and a value of 1 for @force_reinit_subscription.

  2. (Optional) To remove the published object from the database entirely, execute the DROP <objectname> command at the Publisher on the publication database.

Examples (Transact-SQL)

The following example deletes an article from a transactional publication. Because this change invalidates the existing snapshot, a value of 1 is specified for the @force_invalidate_snapshot parameter.

DECLARE @publication AS sysname;  
DECLARE @article AS sysname;  
SET @publication = N'AdvWorksProductTran';   
SET @article = N'Product';   
  
-- Drop the transactional article.  
USE [AdventureWorks]  
EXEC sp_droparticle   
  @publication = @publication,   
  @article = @article,  
  @force_invalidate_snapshot = 1;  
GO  

The following example deletes two articles from a merge publication. Because these changes invalidate the existing snapshot, a value of 1 is specified for the @force_invalidate_snapshot parameter.

DECLARE @publication AS sysname;  
DECLARE @article1 AS sysname;  
DECLARE @article2 AS sysname;  
SET @publication = N'AdvWorksSalesOrdersMerge';  
SET @article1 = N'SalesOrderDetail';   
SET @article2 = N'SalesOrderHeader';   
  
-- Remove articles from a merge publication.  
USE [AdventureWorks]  
EXEC sp_dropmergearticle   
  @publication = @publication,   
  @article = @article1,  
  @force_invalidate_snapshot = 1;  
EXEC sp_dropmergearticle   
  @publication = @publication,   
  @article = @article2,  
  @force_invalidate_snapshot = 1;  
GO  

Using Replication Management Objects (RMO)

You can delete articles programmatically by using Replication Management Objects (RMO). The RMO classes you use to delete an article depend on the type of publication to which the article belongs.

To delete an article that belongs to a snapshot or transactional publication

  1. Create a connection to the Publisher by using the xref:Microsoft.SqlServer.Management.Common.ServerConnection class.

  2. Create an instance of the xref:Microsoft.SqlServer.Replication.TransArticle class.

  3. Set the xref:Microsoft.SqlServer.Replication.Article.Name%2A, xref:Microsoft.SqlServer.Replication.Article.PublicationName%2A, and xref:Microsoft.SqlServer.Replication.Article.DatabaseName%2A properties.

  4. Set the connection from step 1 for the xref:Microsoft.SqlServer.Replication.ReplicationObject.ConnectionContext%2A property.

  5. Check the xref:Microsoft.SqlServer.Replication.ReplicationObject.IsExistingObject%2A property to verify that the article exists. If the value of this property is false, either the article properties in step 3 were defined incorrectly or the article does not exist.

  6. Call the xref:Microsoft.SqlServer.Replication.Article.Remove%2A method.

  7. Close all connections.

To delete an article that belongs to a merge publication

  1. Create a connection to the Publisher by using the xref:Microsoft.SqlServer.Management.Common.ServerConnection class.

  2. Create an instance of the xref:Microsoft.SqlServer.Replication.MergeArticle class.

  3. Set the xref:Microsoft.SqlServer.Replication.Article.Name%2A, xref:Microsoft.SqlServer.Replication.Article.PublicationName%2A, and xref:Microsoft.SqlServer.Replication.Article.DatabaseName%2A properties.

  4. Set the connection from step 1 for the xref:Microsoft.SqlServer.Replication.ReplicationObject.ConnectionContext%2A property.

  5. Check the xref:Microsoft.SqlServer.Replication.ReplicationObject.IsExistingObject%2A property to verify that the article exists. If the value of this property is false, either the article properties in step 3 were defined incorrectly or the article does not exist.

  6. Call the xref:Microsoft.SqlServer.Replication.Article.Remove%2A method.

  7. Close all connections.

Related content