Publication.Remove Method

 

Removes an existing publication.

Namespace:   Microsoft.SqlServer.Replication
Assembly:  Microsoft.SqlServer.Rmo (in Microsoft.SqlServer.Rmo.dll)

NameDescription
System_CAPS_pubmethodRemove()

Removes an existing publication.

System_CAPS_pubmethodRemove(Boolean)

Removes an existing publication even if the Distributor cannot be accessed.

This example deletes a transactional publication.

// Define the Publisher, publication database, 
// and publication names.
string publisherName = publisherInstance;
string publicationName = "AdvWorksProductTran";
string publicationDbName = "AdventureWorks2012";

TransPublication publication;
ReplicationDatabase publicationDb;

// Create a connection to the Publisher 
// using Windows Authentication.
ServerConnection conn = new ServerConnection(publisherName);

try
{
	conn.Connect();

	// Set the required properties for the transactional publication.
	publication = new TransPublication();
	publication.ConnectionContext = conn;
	publication.Name = publicationName;
	publication.DatabaseName = publicationDbName;

	// Delete the publication, if it exists and has no subscriptions.
	if (publication.LoadProperties() && !publication.HasSubscription)
	{
		publication.Remove();
	}
	else
	{
		// Do something here if the publication does not exist
		// or has subscriptions.
		throw new ApplicationException(String.Format(
			"The publication {0} could not be deleted. " +
			"Ensure that the publication exists and that all " +
			"subscriptions have been deleted.",
			publicationName, publisherName));
	}

	// If no other transactional publications exists,
	// disable publishing on the database.
	publicationDb = new ReplicationDatabase(publicationDbName, conn);
	if (publicationDb.LoadProperties())
	{
		if (publicationDb.TransPublications.Count == 0)
		{
			publicationDb.EnabledTransPublishing = false;
		}
	}
	else
	{
		// Do something here if the database does not exist.
		throw new ApplicationException(String.Format(
			"The database {0} does not exist on {1}.",
			publicationDbName, publisherName));
	}
}
catch (Exception ex)
{
	// Implement application error handling here.
	throw new ApplicationException(String.Format(
		"The publication {0} could not be deleted.",
		publicationName), ex);
}
finally
{
	conn.Disconnect();
}

This example deletes a merge publication.

// Define the Publisher, publication database, 
// and publication names.
string publisherName = publisherInstance;
string publicationName = "AdvWorksSalesOrdersMerge";
string publicationDbName = "AdventureWorks2012";

MergePublication publication;
ReplicationDatabase publicationDb;

// Create a connection to the Publisher.
ServerConnection conn = new ServerConnection(publisherName);

try
{
	// Connect to the Publisher.
	conn.Connect();

	// Set the required properties for the merge publication.
	publication = new MergePublication();
	publication.ConnectionContext = conn;
	publication.Name = publicationName;
	publication.DatabaseName = publicationDbName;

	// Delete the publication, if it exists and has no subscriptions.
	if (publication.LoadProperties() && !publication.HasSubscription)
	{
		publication.Remove();
	}
	else
	{
		// Do something here if the publication does not exist
		// or has subscriptions.
		throw new ApplicationException(String.Format(
			"The publication {0} could not be deleted. " +
			"Ensure that the publication exists and that all " +
			"subscriptions have been deleted.",
			publicationName, publisherName));
	}

	// If no other merge publications exists,
	// disable publishing on the database.
	publicationDb = new ReplicationDatabase(publicationDbName, conn);
	if (publicationDb.LoadProperties())
	{
		if (publicationDb.MergePublications.Count == 0 && publicationDb.EnabledMergePublishing)
		{
			publicationDb.EnabledMergePublishing = false;
		}
	}
	else
	{
		// Do something here if the database does not exist.
		throw new ApplicationException(String.Format(
			"The database {0} does not exist on {1}.",
			publicationDbName, publisherName));
	}
}
catch (Exception ex)
{
	// Implement application error handling here.
	throw new ApplicationException(String.Format(
		"The publication {0} could not be deleted.",
		publicationName), ex);
}
finally
{
	conn.Disconnect();
}

Publication Class
Microsoft.SqlServer.Replication Namespace
How to: Delete a Publication (RMO Programming)

Return to top
Show: