Introduction
In this blog we will about to clean up the document library in SharePoint 2013 using PowerShell method. We need to delete all files and folders in document library in our Site Collection for our development purpose.
For that I have created a small PowerShell script to work on this.
Steps
- Start your windows PowerShell on your computer.
- Right click and select Run as administrator option.
- Paste the below script on the PowerShell window and click the enter button.
- Check your SharePoint site page will be created successfully.
Script
- Add-PSSnapin Microsoft.SharePoint.PowerShell
-
- $web = Get-SPWeb -Identity "http://thegowtham.sharepoint.com"
-
- $listname = $web.GetList("http://thegowtham.sharepoint.com /Library")
-
- function DeleteFiles
- {
- param($folderUrl)
- $folder = $web.GetFolder($folderUrl)
- foreach ($file in $folder.Files)
- {
- # Delete file by deleting parent SPListItem
- Write-Host("DELETED FILE: " + $file.name)
- $list.Items.DeleteItemById($file.Item.Id)
- }
-
- }
-
- # Delete root files
-
- DeleteFiles($listname.RootFolder.Url)
-
- # Delete files in folders
-
- foreach ($folder in $listname.Folders)
- {
- DeleteFiles($folder.Url)
- }
-
- # Delete folders
-
- foreach ($folder in $list.Folders)
- {
- try
- {
- Write-Host("DELETED FOLDER: " + $folder.name)
- $list.Folders.DeleteItemById($folder.ID)
- }
- catch
- {
- Write-Host(“Deletion of parent folder already deleted this folder”)
- }
- }
Run the script with the required administrator privilege.
To verify that, go the Pages Library and make sure that new page available or not.