SharePoint: Delete / Purge / Remove Orphaned Sites or Site Collections

If you are here just to know how to delete a site or a site collection: Go to here. If you are here to learn how to find and purge the orphan sites or site collections, go ahead and read the below article.

Microsoft makes it tough with simple things like finding and deleting the orhpaned sites/site collections. There seems to be no well documented way to do this task. After googling for many search strings, below are few resources and steps you can take to clean up your content and config databases.

1. Run the following command:

stsadm -o databaserepair -url http://<URL_of_WindowsSharePointServices_Site> -databasename <name of database that contains the orphan item>

2. If the above command reveals any orhpaned sites, then run the above command with -deletecorruption parameter:

stsadm -o databaserepair -url http://<URL_of_WindowsSharePointServices_Site> -databasename <name of database that contains the orphan item that is to be deleted> -deletecorruption

For more details: read this. Bloggers say that these seem to do nothing but might be doing something.

3. If the above stsadm command doesnt reveal any orphan sites, then there is a DB script provided in this blog post, which help you find out the orhpaned sites. Its not a good practice to run queries against the sharepoint database but this really works so who cares.

Use MSDB
Drop table orphanlist

CREATE TABLE [dbo].[orphanlist](
[farm] [varchar](250) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[databasename] [varchar](250) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[SiteID] [uniqueidentifier] NULL,
[sitepath] [varchar](250) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[type] [varchar](250) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
)

drop table orphan_hopper
declare
@dbname as varchar(250),
@cmdstr as varchar(2000),
@dbid as varchar(250),
@configdb as varchar(250)

/** only change the following line and nothing else, change spskills_config_db to your config db name **/
select @configdb = ‘spskills_config_db’

/** Change nothing below this line **/
select @cmdstr =
‘select distinct b.name as ”databasename”, b.id as ”dbid” into orphan_hopper
from
[' + @configdb + '].dbo.sitemap as a inner join
[' + @configdb + '].dbo.objects as b on a.databaseid=b.id inner join
[' + @configdb + '].dbo.objects as c on c.id=a.applicationid inner join
[' + @configdb + '].dbo.objects as d on b.parentid=d.id inner join
[' + @configdb + '].dbo.objects as e on d.parentid=e.id ‘
exec (@cmdstr)

DECLARE DBCursor CURSOR For
Select databasename, dbid
From orphan_hopper

OPEN DBCursor
FETCH NEXT FROM DBCursor into @DBName, @dbid

WHILE @@FETCH_STATUS =0
BEGIN
INSERT INTO orphanlist([Type], farm, databasename,[sitepath], SiteID)
EXEC
(‘
select ”Potential ConfigDB orphan:” + ”’+@dbname+”’ as [Type], ”’+@configdb+”’ as [farm], ”’+@dbname+”’ as [databasename],path as [sitepath], id as [SiteID] from ['+@configdb+'].dbo.sitemap where id not in (select id from ['+@dbname+'].dbo.sites) and databaseid = ”’+@dbid+”’
union
select ”Potential ConfigDB orphan:” + ”’+@dbname+”’ as [Type], ”’+@configdb+”’ as [farm], ”’+@dbname+”’ as [databasename],path as [sitepath], id as [SiteID] from ['+@configdb+'].dbo.sitemap where id not in (select siteid from ['+@dbname+'].dbo.webs where parentwebid is null) and databaseid = ”’+@dbid+”’
union
select ”Potential ContentDB orphans:” + ”’+@dbname+”’ as [Type], ”’+@configdb+”’ as [farm], ”’+@dbname+”’ as [databasename],fullurl as [sitepath], siteid as [SiteID] from ['+@dbname+'].dbo.webs where parentwebid is null and siteid not in (select id from ['+@configdb+'].dbo.sitemap where databaseid = ”’+@dbid+”’)
union
select ”Potential ContentDB orphan:” + ”’+@dbname+”’ as [Type], ”’+@configdb+”’ as [farm], ”’+@dbname+”’ as [databasename],fullurl as [sitepath], siteid as [SiteID] from ['+@dbname+'].dbo.webs where parentwebid is null and siteid not in (select id from ['+@dbname+'].dbo.sites)
‘)
FETCH NEXT FROM DBCursor into @DBName, @dbid
END
CLOSE DBCursor
DEALLOCATE DBCursor

select * from orphanlist

4. If either of the above show that your sharepoint db has orhpaned objects, the recommended practice is to detach and re-attach the content database for that particular web application. The detach and attach process can be done in the central adminsitration. Read this blog post which I think mentions how to detach/re-attach using the SPS 2003 Centram Admin. In SPS 2007, just find out the Manage Content Database section under which you should be able to find the web application and then there is a way to re-attach the content db. If you face any problems re-attaching, try to do the same using stsadm tool.

7 Responses

  1. [...] Deleting / Purging / Removing Orphaned Sites or Site Collections [...]

  2. Hi! I was surfing and found your blog post… nice! I love your blog. :) Cheers! Sandra. R.

  3. Sign: gfndc Hello!!! hwfmy and 3924eebtmcsbxy and 3461 : I love your site. :) Love design!!! I just came across your blog and wanted to say that Ive really enjoyed browsing your blog posts.

  4. [...] SharePoint: Delete / Purge / Remove Orphaned Sites or Site Collections November 2007 3 comments [...]

  5. How about supplying an actual example of what the parameters are suppose to hold instead of just quoting Microsoft’s web site!

  6. How about Site Templates in SharePoint 2010??? I opened our Solutions Gallery and deleted sites without the new 2010 prerequisite of deactivate ing the site templates first. Now they appear grayed under the Custom tab under “Select a template:” and also under the new Icon based QUI in 2010 sites. Are these considered orphaned sites and be dealt with similarly as mentioned above?

  7. Hi i would like to thank you for such a fantastic blog. I was trying to find solution for orphan sites issue and your commands really helped me. Thanks once again and keep posting such a useful blog posts.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.