How to remove files in a SharePoint library?

4 min read.

This article explains how you can remove a big batch of files from a library using either the UI or PnP PowerShell. And when I say “big”, I mean, roughly, “as many as possible”, without removing the library or touching the folders in it.

I’m not trying to speedrun SharePoint, here, I’m just trying to get rid of some files without resorting to shortcuts.

Please join me on this adventure!

(This article was originally scheduled to be published on Tuesday the 13th of February, but the scheduled publishing somehow failed – so now it goes out on Sunday instead, as I don’t want to break my 4-year-long weekly publishing streak 😅)

Background

Every now and then comes a time, when I have a few hundred thousand files in a library, and need to cull it down a little bit. But whenever the number of items in a list or library exceeds 5000 items, you start running into some limitations in SharePoint.

Just to clarify what kind of a library we’re dealing with here, take a look at the screenshot below:

This article does not go into the logic on how to filter the files for removal: maybe you have a view for them, or maybe you have the exact right query for Get-PnPListItem – but I’ll skip all of that, and just get to the part where we need to remove thousands and thousands of files.

So far, so good, right?

Problem

So with SharePoint’s fancy UI, you can just navigate to a directory/subdirectory in the list and click the “Toggle selection for all items” button to select all items and remove them. Easy, right?

And now comes the issue. Even if your list had thousands and thousands of items – clicking this will actually only select the first visible batch! So whatever you’re trying to do to all those items, You’re only getting a few of them. 100 by default.

Removing thousands of files like this is going to be bothersome through this slow UI.

Of course we could use PowerShell (and I’ll show a sample of that at the end), but what if we just want to use the UI and be as quick as possible?

Solution

We’ll want to modify your list or library’s view to allow for larger item limits. That’ll enable you to Here’s how to do that!

1. Navigate to your List or Library’s Settings

The natural first step is, of course, to navigate to your list, and then click “Library Settings” from the gear menu to access your list or library’s settings.

2. Select your preferred view for editing

Scroll down to “Views”-section, and select the view you want to edit (to have a higher view limit).

3. Expand “Item Limit” and change it to 1000

I’m not sure if this is documented, but 1000 is the maximum. At least at the time of writing this.

4. Time to try again!

This time, selecting a thousand items at once is fast, and removing them is also reasonably quick.

Granted, occasionally you might have some of the removals time out. But then you just retry.

That’s probably the fastest way to remove items (as opposed to folders or the whole list at once) from a library using SharePoint UI :) And if you know a faster way (without messing with the folders), let me know!

I have a suspicion, that this would be faster (or at least more hands-off) with a Power Automate Flow, but I didn’t want to figure that out today.

Bonus: What happens if you set “Item limit” to 5000 in a SharePoint List View and try to remove them all at once?

Probably doesn’t come as a surprise, but I did try setting the Item Limit to 5000 items, selected them all, and then hit “Delete”.

Yet another thing that a perfectly sane person like myself would do. Of course you can do this! And you can also hit “Delete”. And I did.

Lo and behold:

But, it will fail. And looking at the console, you’ll see this:

plt.listviewdataprefetch.js:2 Uncaught (in promise) GetDataError: The current change set contains too many operations. A maximum number of '1000' operations are allowed in a change set.
    at t.a (plt.listviewdataprefetch.js:2:125041)
    at t [as constructor] (plt.listviewdataprefetch.js:2:59702)
    at new t (plt.odsp-common.js:1:114751)
    at O (plt.odsp-common.js:1:111142)
    at plt.odsp-common.js:1:111325
    at e._onReadyStateChange (plt.odsp-common.js:1:495932)
    at e.<anonymous> (plt.odsp-common.js:1:493097)
    at XMLHttpRequest.f (plt.odsp-common.js:1:54843)

Alright – so 5000 is too much for a batch of list item operations. That’s why I settled for a more reasonable 1000 item batch.

Also – don’t try to switch your lists to Classic Experience. That has a limit of just 100 selected items at once. Boo.

Remove all items in a list in batches of 5000

While you can get up to a reasonable speed with the UI (as long as you change the list view settings), using PowerShell is generally speaking always faster.

You can do something like below:

Get-PnPListItem -PageSize 5000 -List "Shared Documents" | Remove-PnPListItem -Recycle -force

That’s all for today!

mm
0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
most voted
newest oldest
Inline Feedbacks
View all comments