How to avoid attachments from using up too much storage in attachment table when sending out daily report notifications


Depending on business requirements, there can be a use case where significant number of Email reports need to be sent that have large report attachment files.

Over time, all those attachments will use up a lot of storage space in the database.

Here is automated solution that be implemented to flag some of those attachments for deletion without deleting attachments directly.



Please note that the suggestion proposed in this article adds on to the out of the box features documented below.

Data archiving / Email Retention plugin: Data archiving

Email retention: Email retention


It is possible to delete Email records and their file attachments will cascade delete.

Here are some sample basic queries to get started with like:


Additional filter criteria should be used to avoid deleting wrong records such as :

Here is a sample script that may be run as part of an automated scheduled job.

Please note that this is just an example, and it should be further extended and tested before, never run this in production without testing.

It would be worthwhile considering to add auditing so that records would only be flagged for deletion and their sys_id added to a temporary table and reviewed by and administrator before finally deleting them.

var gr = new GlideRecord('sys_email');
//No target table
// sys_report table
// gr.addEncodedQuery('target_table=sysauto_report^typeINsend-failed,send-ignored,sent');

// Test row count, rowcount can impact performane if there are many.
// Add conditions or auditing code here before deleting.
// Once you are sure, then delete with that code.