1

I have one root site and multiple sub-sites. I have to find out all the documents of last year and need to delete them from the sharepoint because they are consuming much space. I came to know that It is possible by using powershell but I am new in that.

Please suggest how to achieve it.

Thanks.

1

Try below code:

Add-PSSnapin "Microsoft.SharePoint.PowerShell"
$Today = (Get-Date).AddYears(-1)

$expireyDate = [microsoft.sharepoint.utilities.sputility]::CreateISO8601DateTimeFromSystemDateTime($Today);

$web = Get-SPWeb http://SharePointSite/SubSite
$sourceList = $web.Lists["Documents"]

$spQuery = New-Object Microsoft.SharePoint.SPQuery;
$camlQuery = "<Where><Lt><FieldRef Name='Modified'/><Value Type='DateTime' IncludeTimeValue='False'>$expireyDate</Value></Lt></Where>";
$spQuery.Query = $camlQuery
$sourceItems = $sourceList.GetItems($spQuery)

foreach($item in $sourceItems)
{
$File = $item.File
$File.Delete()
}
$web.Dispose()
0

Yes you can achieve this using below PowerShell script:

$web = Get-SPWeb http://siteurl
$folder = $web.GetFolder("Library Name")
$files = $folder.Files

foreach($file in $files)
{    
     $file.Delete();        
}

$web.Dispose()

and to apply the filter as you need you can pass the caml query object and retrieve the items:

$caml='<Where>
    <Leq>
        <FieldRef Name="Created" />
        <Value IncludeTimeValue="TRUE" Type="DateTime">2015-12-31T02:32:20Z</Value>
    </Leq>
</Where>'

$query=new-object Microsoft.SharePoint.SPQuery 
$query.Query=$caml | Write-Output

$items=$list.GetItems($query) 
$listItemsTotal = $items.Count;

for($x=$listItemsTotal-1;$x -ge 0; $x--)
{
    $items[$x].Delete()
}

$web.Dispose() 
0

Looks like you need to delete all the documents of Last year and from all the libraries of all sub site. Please note that this is very risky operation and please make sure you perform backup or proceed with caution.

Assuming you have name of libraries which has this document is something like below.

$Libraries = @("Documents","Shared Documents","Documents1","Documents2")

using code given by @Viraj as reference and modified so that it iterates through all the subsites within a site collection and find libraries listed in above array and delete document which are year old

$Today = (Get-Date).AddYears(-1)

$expireyDate = [microsoft.sharepoint.utilities.sputility]::CreateISO8601DateTimeFromSystemDateTime($Today);

$spsite = Get-SPsite $sc 
$webs = Get-SPWeb $sc


$camlQuery = "<Where><Lt><FieldRef Name='Modified'/><Value Type='DateTime' IncludeTimeValue='False'>$expireyDate</Value></Lt></Where>";

$Libraries = @("Documents","Shared Documents","Documents1","Documents2");

foreach ($web in $site.AllWebs)
{   
    foreach ($lib in $Libraries )
    {
        $sourceList = $web.Lists.TryGetList($lib)
        if($sourceList -ne $null)
        {
            $spQuery = New-Object Microsoft.SharePoint.SPQuery;
            $spQuery.Query = $camlQuery
            $sourceItems = $sourceList.GetItems($spQuery)

            foreach($item in $sourceItems)
            {
                $File = $item.File
                $File.Delete()
            }
        }
    }            
}
0

How do you delete files and folders from SharePoint document library if it does not match contents inside folder in file share? Actually i wanted to sync everything from file share to SharePoint. I am using someone else's code to achieve that, but would like to add delete block as well. copy to SharePoint from file share

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.