Delete all Records
The setup
Had a support call escalated to me a few days ago, customer wanted a method to delete all records. Sounds interesting I thought, maybe a bit of an edge case, so I suggested they do this:
TrimMainObjectSearch search = new TrimMainObjectSearch(database, BaseObjectTypes.Record); search.SelectAll(); foreach (Record record in search) { record.Delete() }
Push back
Of course the customer pushed back, they wanted a more efficient way to delete all records. I happened to mention this in our daily team meeting only to be informed by a colleague that we do have a 'delete all' function, in fact two.
TrimMainObjectSearch search = new TrimMainObjectSearch(database, BaseObjectTypes.Record); Record.BulkDeleteAndDeleteContents(search, true); TrimMainObjectSearch search = new TrimMainObjectSearch(database, BaseObjectTypes.Record); Record.BulkDeleteAndUnlinkContents(search, new Location(database, 123), true);
These functions are more efficient than my foreach loop given that they bypass all the object cache logic. Seems I learn something new every day!
Example
This code will delete all records that or not container type records. It will only delete records if there are no problems with doing so, things that might prevent records being deleted include:
- one or more matching records being under legal hold,
- one or more matching records having a client relationship with a record not being deleted.
TrimMainObjectSearch search = new TrimMainObjectSearch(database, BaseObjectTypes.Record); search.SetSearchString("not type:[behaviour:Folder,Series,Box,PaperFolder]"); Record.BulkDeleteAndUnlinkContents(search, database.CurrentUser, false);
Warning
I don't need to say this but don't just copy and paste the code above, make sure you test your search to ensure you only delete what you want to delete. Take a backup first of course.
Rationale
So what was the customer trying to achieve? Turns out the customer was HP and we had been testing HPRM datasets for the new companies. When the split actually happens they want to clean out all test data, leaving containers only, and start again with production data.