SharePoint – Cleanup & Maintenance

SharePoint Cleanup

As administrator you need to do regular cleanups.

In order to keep everything running smooth and have your database not to be oversized with digital waste.

A few things to keep track of :

1. Delete List Items that reflect a logging of some kind, needs to be cleaned.

cls
if((Get-PSSnapin | Where {$_.Name -eq "Microsoft.SharePoint.PowerShell"}) -eq $null) {
    Add-PSSnapin Microsoft.SharePoint.PowerShell;
}

$sourceWebURL = "http://www.YourSharePoint.com/"
$sourceListName = "LogList"

$spSourceWeb = Get-SPWeb $sourceWebURL
$spSourceList = $spSourceWeb.Lists[$sourceListName]

$spSourceItems = $spSourceList.Items #| where {$_['ID'] -eq 1}

write-host $spSourceList.Items.count

$i = 0

$spSourceItems | ForEach-Object {

    Write-Host $i $_['ID'] $_['Title'] $_['Date']

    $itemID = $spSourceList.GetItemById($_['ID'])
    $itemID.delete()

    # $itemID.Recycle() # Moves it to the recyclebin instead
    $i++
}

2. Delete List Item Versions ?

CLS
if((Get-PSSnapin | Where {$_.Name -eq "Microsoft.SharePoint.PowerShell"}) -eq $null)
{
    Add-PSSnapin Microsoft.SharePoint.PowerShell;
}
# get site
$site = new-object Microsoft.SharePoint.SPSite("http://www.YourSite.com/")

# loop through webs
foreach ($web in $site.AllWebs)
{
  # loop through all lists in web
  foreach ($list in $web.Lists)
  {
    # examine if BaseType of list is NOT a Document Library
    if ($list.BaseType -ne "DocumentLibrary")
    {
      # forget the rest and return to top
      continue
    }
    # loop through each item
    foreach ($item in $list.Items)
    {
      # work with the file object as we're in a document library
      $file = $item.File
      Write-host $file.name
      Write-host " "
      Write-host $file.Versions
      delete all versions
      #$file.Versions.DeleteAll()
    }
  }
}

$web.Dispose();
$site.Dispose();

3. Maybe remove large attachments that are found double in your library.

CLS

if((Get-PSSnapin | Where {$_.Name -eq "Microsoft.SharePoint.PowerShell"}) -eq $null)
{
    Add-PSSnapin Microsoft.SharePoint.PowerShell;
}

$web=Get-SPWeb -identity "http://wss.YourSite.com/"

$list=$web.lists["YourList"];

foreach($item in $List.Items)
  {
        for($i=$item.Attachments.count-1; $i -ge 0; $i--)
        {
             if($item.Attachments[$i].endswith(".docx"))
             {
            write-host "File Deleted:" $item.Attachments[$i]
               #$Item.Attachments.Delete($item.Attachments[$i])
              } 

        }
          # $item.update()
  }

3. Delete Workflow History needs to be removed over time. In this case older then 60 Days.

cls
if((Get-PSSnapin | Where {$_.Name -eq "Microsoft.SharePoint.PowerShell"}) -eq $null) {
    Add-PSSnapin Microsoft.SharePoint.PowerShell;
}

$sourceWebURL = "http://www.YourSite.com/"
$sourceListName = "Workflow History"

$spSourceWeb = Get-SPWeb $sourceWebURL
$spSourceList = $spSourceWeb.Lists[$sourceListName]

$spSourceItems = $spSourceList.Items | where {$_['Created'] -gt (get-date).AddDays(-60)} 

write-host $spSourceList.Items.count "in the list"
write-host " "

$i = 0

$spSourceItems | ForEach-Object {
    Write-Host $_['ID'] $_['List'] $_['WorkflowAssociation'] $_['Created'] $_['Description']
    $itemID = $spSourceList.GetItemById($_['ID'])
    $itemID.delete()
    # $itemID.Recycle() # Moves it to the recyclebin instead
    $i++
}

write-host " "
write-host "$i Selected"

4. Cleanup the Recycle Bin

cls
if((Get-PSSnapin | Where {$_.Name -eq "Microsoft.SharePoint.PowerShell"}) -eq $null) {
    Add-PSSnapin Microsoft.SharePoint.PowerShell;
}

(Get-SPSite "http://YourSite/").RecycleBin | ?{$_.Title -match "System.NullReferenceException"}

(Get-SPSite "http://YourSite/").RecycleBin.DeleteAll()

After cleaning up a bit your can shrink your Content DB’s a bit to save some space

image

 

Enjoy

Advertisements

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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: