This sample console application uses the SharePoint .NET client-side object model (CSOM) to delete old versions of all files from multiple folders from your SharePoint or OneDrive for Business subscription.
This is not production code, and it deletes files from your cloud storage. Use at your own risk!
This console application executes the following steps:
- Connects to your OneDrive for Business or SharePoint site collection.
- Finds the
Documents
document library. - Iterates through the specified subfolder paths in the document library.
- It runs CAML queries in every folder to retrieve the documents (files). In a single query maximum 100 documents are retrieved, and the query is executed again and again until all documents are processed.
- If a document has multiple versions, they are deleted.
To use this sample follow these steps:
- Clone this repository.
- Open the
OneDriveVersionCleanerSolution.sln
solution in Visual Studio (written and tested with VS 2017). - At the top of the
Main
method specify the URL of your cloud storage, your username and password, and the paths to the folders you want to process. If you have multifactor authentication enabled (you do, right?) you need to create an application password first (check the blog post mentioned in the Read more section below). - In line 124 uncomment the
file.Versions.DeleteAll()
call to actually delete the old versions. - Run at your own risk.
You can read the full story behind this code and other options to reduce the OneDrive storage space needed for your files on my Delete old document versions from OneDrive for Business blog post.
This sample was created by György Balássy.