Deleting a previously published database

You sometimes need to restore a database previously published, in order to reproduce a problem or if you must work with the same data or amount of data.

This database is not published anymore but you can still see the traces of the previous publication (triggers on published tables or replication system tables)

When you try to remove this database you could receive the following error:
Cannot drop the database ‘DatabaseName’ because it is being used for replication.
Here is a way to remove this database from your instance:

DECLARE @DataBaseName varchar(30)
DECLARE @sql varchar(max)

SET @DataBaseName = 'DatabaseName'

----------------------------------------

-- Suppression de la DB

----------------------------------------

SET @sql ='ALTER DATABASE [' + @DataBaseName +'] SET OFFLINE WITH ROLLBACK IMMEDIATE'

EXEC (@sql)

SET @sql ='DROP DATABASE [' + @DataBaseName +'] '

EXEC (@sql)

GO