We migrated around 25000 files from network share to SharePoint Online. However, of these, around 1400 of them were migrated to the root folder which shouldn't have happened and we would like to delete these.

My SharePoint site is https://myclient.sharepoint.com/sites/dynamics365production/. Within this, I have a library called as Opportunity and within that I have a number of folders each of which has zero or more documents. However, there are around 1400 files available at https://myclient.sharepoint.com/sites/dynamics365production/opportunity.

What I want to be able to do is, delete only files which are at this level. The files which exist in the folder https://myclient.sharepoint.com/sites/dynamics365production/opportunity/FolderA and https://myclient.sharepoint.com/sites/dynamics365production/opportunity/FolderB should continue to exist.

How do I do it?

Here is what I have tried to do so far.

  1. If I try to create a view, so that I can sort and see all the files at the top I get the following error message.

This view cannot be displayed because it exceeds the list view threshold (5000 items) enforced by the administrator.

To view items, try selecting another view or creating a new view. If you do not have sufficient permissions to create views for this list, ask your administrator to modify the view so that it conforms to the list view threshold.


If your issue is with limiting the view to less than 5000 items, you can apply a view filter such as "Name Starts With A" ... then "B"... and so on. If your file naming can be "chunked" into <5000 groups in an easier way go with that instead.

It won't be quick, but you can eventually clear out all of those items page by page, and chunk by chunk.

To achieve this quickly, you can write code using CSOM to clear out the unwanted items. I wrote a script that clears ALL items from a list/library. But it could easily be adapted to check for the folder name and skip anything beyond the root. Here's a snippet:

    public void ClearListItems(Guid listId)
        ListItemCollectionPosition licp = null;
        var list = Client.Web.Lists.GetById(listId);

            var query = new CamlQuery
                // need to delete in chunks or sharepoint will time out (max 250)
                ViewXml = @"<View><ViewFields><FieldRef Name='Id'/></ViewFields><RowLimit>250</RowLimit></View>",
                ListItemCollectionPosition = licp

            var items = list.GetItems(query);


            licp = items.ListItemCollectionPosition;

            foreach (var item in items.ToList())


        } while (licp != null);

/// My "Get" function is as follows

    public T Get<T>(T item, params Expression<Func<T, object>>[] includes) where T : ClientObject
        Client.Load(item, includes);

        return item;

Also, the "Client" is of type "SPClient" from the CSOM.

| improve this answer | |

If you don't want to use a script, another option is to use the OneDrive Sync Client. Assuming you are on Win10 > 1709, and have the latest OneDrive client... the client won't actually download your files locally. So you just navigate to the library, click sync, confirm access to use the client, then mass select and delete in Window Explorer.


Edit: the link above still outlines downloading all files from the DocLib. Here is info regarding "On Demand":


| improve this answer | |

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.