Forum Discussion
Powershell script to delete all versions of files in a sharepoint site from all libraries
NicolasKheirallah Greg_from_Pond Greg_from_Pond rohankapz
We are continuously adding space to SP Online site, because my SharePoint online is constantly running out of space, we have huge libraries with 1000 GB and 10000 Versions.
looking for best solution for high SharePoint storage Issue. and the best practices to Maintain versions if we want, also.
My Question:
with retention policy in place, therefore versions with retention policy will be deleted old versions and limit 50 versions by the script above. If not, what is the best course of action for my problem?
how to clear versions of existing library files without effecting latest file versions. Also, we have retention policy in place for 7 Years ?
Hi,
I would not recommend using a script like this as part of a live/ongoing system. The better solution would be to use a Document Library template for the site, and set the maximum versions to a fairly low number (acceptable to your business). If you have a 3rd party backup, that number can be as low as 5-10 and still provide good protection.
For the retention policy, you can have automated rules in place at folder (or file) level to move or delete files after 7 years.
I tend to have a separate SharePoint site called ARCHIVE, or similar, with restricted access and any files older than the retention period get moved there. That in turn has an auto-delete rule to avoid compliance issues.
This can be achieved fairly easily using tags or labels, standard retention policies or Power Automate flows.
I hope that helps
Greg
NB - that script doesn't work in it's current form. I ended up just moving the files to a separate site and removing the versions at the same time. I saved 800GB overnight,
- Feb 07, 2024
Just a tip but intelligent versioning is comming soon, which should solve the versioning issue:
Edit:
I've created a script that enables Intelligent versioning and applies it to each site, it also starts a job that deletes all versions for all files older than 365 days
How to Free Up Storage in SharePoint Online Using Intelligent Versioning and PowerShell