I have created a script to delete all documents inside a library, including documents in folders and the folders themselves.

However, it seems that the script will not remove documents that have no checked-in version, that is documents that were uploaded to sharepoint but were nevere checked in.

Checking-in these documents is not an option, since several required properties will be missing.

I have included a snippet below with part of my code, just to demonstrate how the documents are deleted.

$folder = $web.GetFolder($folderUrl)              
foreach ($file in $folder.Files) 
  • Do you get any error message? – Naim Murati Jan 3 '14 at 13:02
  • No, there are no exceptions. It just won't find the file, as if it does not exist. – yandr Jan 4 '14 at 14:00

Documents uploaded and never checked-in can be seen only by the uploader. You have to take ownership over those documents first.

You can do that manually or by code:

  1. Manually

Go to Document Library Settings -> Manage files that have no checked in versions. Select the files you want and click 'Take Ownership of Selection'.

Run your script again to delete those files.

  1. With PowerShell script

For the document library take the checked out files


And for each of them take over the ownership


Then run your script to delete the files.

  • This could work. I will check it out and mark it as correct answer if it succeeds. – yandr Jan 6 '14 at 8:52

you need to check in the folder first before removing it: someone has gone through the hassle for you:

please read the full blog:



$site = New-Object Microsoft.SharePoint.SPSite("http://SP2010")

$root = $site.allwebs[0]

$folder = $root.GetFolder("My Document Library")


# Function Set-CheckInFolderItems is a recursive function 

# that will CheckIn all items in a list recursively


function Set-CheckInFolderItems([Microsoft.SharePoint.SPFolder]$folder)


    # Create query object

    $query = New-Object Microsoft.SharePoint.SPQuery

     $query.Folder = $folder

    # Get SPWeb object

    $web = $folder.ParentWeb

    # Get SPList

    $list = $web.Lists[$folder.ParentListId]

    # Get a collection of items in the specified $folder

    $itemCollection = $list.GetItems($query)

    # If the folder is the root of the list, display information

    if ($folder.ParentListID -ne $folder.ParentFolder.ParentListID)


        Write-Host("Recursively checking in all files in " + $folder.Name)


    # Iterate through each item in the $folder 

    foreach ($item in $itemCollection)


        # If the item is a folder

        if ($item.Folder -ne $null)


            # Write the Subfolder information

            Write-Host("Folder: " + $item.Name + " Parent Folder: " + $folder.Name)

            # Call the Get-Items function recursively for the found sub-solder

            Set-CheckInFolderItems $item.Folder


        # If the item is not a folder

        if ($item.Folder -eq $null)


            if ($item.File.CheckOutType -ne "None")


                if ($item.File.Versions.Count -eq 0)


                    # Check in the file

Write-Host "Check in File: "$item.Name" Version count " $item.File.Versions.Count -foregroundcolor Green

                    $item.File.CheckIn("Checked in By Administrator")






    $web = $null



Set-CheckInFolderItems $folder
  • I will try this, but in principle it should not work. How can you check-in a file that does not have the required metadata? – yandr Jan 4 '14 at 13:58
  • I am not 100% sure, but I think you can easily check in anything programmatically. If you don't fill some required fields you'll receive an error only when you are using GUI. there shouldn't be any problems when you are doing it in PowerShell or C#. – Denis Molodtsov Jan 4 '14 at 19:41

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.