I need to have a rule to cleanse old email messages, but keep the case & activity records. It seems this is possible to do manually (find the case, click into the task, click into the related email message > Delete) but I'd like to set up a rule to cleanse in an ongoing manner.

Ideally each night, I want to be able to delete related email messages for cases which have been "closed" for over 60 days. Any recent/open cases we would want to keep everything. We have a backup system for our instance so that if we needed to find a very old email we could dive into the archive.

What would be the best way to go about this? Would it be an Apex trigger, a workflow rule etc?


You can use the Process Builder (plus a Headless Flow) to do this, or you can write a Schedulable class that runs daily.

As a Process Builder, first build a Flow that queries all Email Messages for cases closed more than 60 days, then delete the list. Create a custom object (requires no custom fields, may be auto-number generated), then create a Process Builder that runs 24 hours after record creation that (a) calls the Flow from above, and (b) creates a new custom object record. This creates a chain of Process Builder that will run indefinitely.

As a schedulable class, simply query the Email Message objects for cases closed at least 60 days, and then delete them. This is clearly less complicated, but requires writing code (~6 lines of code, plus ~15 lines of unit test code). You can then schedule this to run daily to purge your old emails.

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.