Restore a previously restored SharePoint Site Collection with Restore-SPSite

In SharePoint you get an error if you try to restore a site collection that war previously restored. I always make a site collection backup of a site while I'm developing directly on a site and if i make any mistakes I restore the back with the Powershell command Restore-SPSite.

Common error messages were:


Restore-SPSite : The operation that you are attempting to perform cannot be completed successfully.  No content databases in the
web application were available to store your site collection.  The existing content databases may have reached the maximum number
 of site collections, or be set to read-only, or be offline, or may already contain a copy of this site collection.  Create anoth
er content database for the Web application and then try the operation again.


Reason

The reason for this problem is that you can't restore sites or web which have the same id. SharePoint don't remove all ids when you delete a site collection. (Don't know why)

Solution

In all other blogs you'll see that you need to create a new content datase or a new webapplication. There is an easier way.

First check if the site is already deleted. When you delete a site collection without PowerShell parameter -GradualDelete $false the site is deleted gradually. That means a timer job will delete the site in near future but the site is marked a deleted.

To check if the site is already deleted use the PowerShell Command "Get-SPDeletedSite".


Now we'll find out for later which content database hosts the sitecollection. Select the "DatabaseId" and type following command:

Get-SPContentDatabase | Where {$_.Id -eq "02cf20b1-b312-4111-98b9-303b8113a71d" }

Pick the name of the content database for later.

If your site is listed there, delete the site now with the command:

Remove-SPDeletedSite -Identity 8489a07c-1b1a-485e-9e7d-b0c4ffbf26b5

The number after Identity is the SiteID.

Now you need to open the "SQL Server Management Studio" to login into the SharePoint Database. We not will change anything here. We use the Studio to find the deleted Site Id.

Click on the Content Database which contains the previously deleted site and open tables. Right click on "dbo.AllWebs" and select "Select Top 100 Rows".



In the table there is a "FullUrl" attribute. Search the url of your deleted site. If you found it copy the Guid in the ID Attribute.


Now open the "SharePoint 2010 Management Console" and enter following stsadm command:

stsadm -o deleteweb -webid <ID of  the Site> -databasename <Database Name> -databaseserver <DB Server>

Example:

stsadm -o deleteweb -webid E3E814F2-8819-4132-854B-7C7548EB2229 -databasename Main_Ct01 -databaseserver NT7262


Now you're able to restore your backuped site collection again.

Comments

KCRyan said…
Your directions are much simpler than what I've done in the past to get around this problem. Question: Is it possible to delete the Site ID with PowerShell, or is this one of those commands that is easier with Stsadm?
Baris Bikmaz said…
I searched also for an identical PowerShell Command, but all commands I've tried didn't work the same way as the stsadm command. The PowerShell commands always return that the Site is already deleted.
Unknown said…
Hello
What about when you have a response like:
Database "WSS_Content_sh_ws_int" is not associated with this Web application

and i´m sure the database is right spell checked
Difference is s HNSC enviroment.

Thanks
Baris Bikmaz said…
I don't know exactly what you're doing but it seems, that your database is not attached to your web application. Try to attach it with Mount-SPContentDatabase. If your Database is already attached, try to detach and attach your content database again. This worked for me in most of the cases.
Unknown said…
Where are the tables located within SQL Server Management Studio?
Baris Bikmaz said…
Hi Paul,
Thanks for the question. I'll update my post.
Connect to your SharePoint SQL Database with the SQL Management Studio, open Databases. You'll see all the child Databases. If you have multiple content databases, you have to know in which content database your sitecollection was. Open the Content Database and then tables. There you'll see the dbo.AllWebs table.

Popular posts from this blog

Open SharePoint 2010/2013 Display, Edit, New Forms in Modal Dialogs

Using WebDav on IIS 7 to access UNC Paths

Ways to redirect http requests in SharePoint