Database clean up process

sns 9,236 Reputation points
2023-03-26T15:12:29.7566667+00:00

We have databases in SharePoint and same are there in SQL as well.

1st question:

We need to remove the SharePoint databases from SharePoint server first and then from SQL server, am I correct? Please confirm

2nd question:

is there any command which will give only SharePOint site collection list from farm along with DB name and export it to CSV file.

Please suggest, thank you.

SharePoint Server
SharePoint Server
A family of Microsoft on-premises document management and storage systems.
2,333 questions
0 comments No comments
{count} votes

Accepted answer
  1. AllenXu-MSFT 19,456 Reputation points Microsoft Vendor
    2023-03-27T02:29:55.9033333+00:00

    Hi @sns,

    1. Yes, you need to delete unused web applications or site collections firstly in SharePoint Server and then delete corresponding databases from SQL Server.
       If ((Get-PSSnapin | where {$_.Name -match "SharePoint.PowerShell"}) -eq $null)    
       {    
           Add-PSSnapin Microsoft.SharePoint.PowerShell    
       }    
       Clear-Host    
           
              
       $webapps = Get-SPWebApplication    
       $Output = "C:\data.csv" #Output file    
            
           
       $SiteDataCollection = @()  # Array to store data    
       foreach ($webapp in $webapps){    
           
       $webappurl = $webapp.URL;    
       $webappname=$webapp.DisplayName    
       $sites=get-spsite -limit all -WebApplication $webapp.URL    
           
       foreach ($site in $sites)    
       {    
       $ContentDB = Get-SPContentDatabase -site $site.url    
           
                   $SiteData = New-Object PSObject 
                   $SiteData | Add-Member -type NoteProperty -name "Site URL" -value $site.url       
                   $SiteData | Add-Member -type NoteProperty -name "Database Name" -value $ContentDB.Name     
                   $SiteData | Add-Member -type NoteProperty -name "Database Size" -value $($ContentDB.DiskSizeRequired /1024/1024)              
                       
                   $SiteDataCollection += $SiteData    
         
       }    
       }    
           
       $SiteDataCollection | Export-Csv -Path $Output -Force    
           
       Write-Host "Successfully Completed" -ForegroundColor DarkRed 
       
    

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.