need to delete files from sys_attachment table (sys attachement report)

Jayaprakash P
Tera Contributor

Hi,

can someone helps us with the script to delete record from sys attachment table only for the reports been sent.

if report has an attachment and that schedule report send via email. We are trying to see the possibility of deleting attachment record which is associated with email send.

 

Thanks & Regards,

Jayaprakash

8 REPLIES 8

Ankur Bawiskar
Tera Patron
Tera Patron

@Jayaprakash P 

you can run a daily scheduled job and query "sys_attachment" table with some unique identifier and then delete

OR
you can have after update Business rule on sys_email table and check if this email was triggered for some scheduled report and if yes then search in sys_attachment and then delete it

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Jayaprakash P 

after update business rule on sys_email

Condition: Target table == 'sysauto_report' && Type == Sent

Script:

// query sys_email_attachment with the email record and delete it

var gr = new GlideRecord('sys_email_attachment');
gr.addQuery('email', current.sys_id);
gr.query();
if (gr.next()) {
    gr.deleteRecord();
    var att = new GlideRecord('sys_attachment');
    att.addQuery('table_sys_id', current.sys_id);
    att.query();
    if (att.next()) {
        att.deleteRecord();
    }
}

AnkurBawiskar_0-1745234251575.png

 

AnkurBawiskar_1-1745234265721.png

 

AnkurBawiskar_0-1745235021983.png

 

I hope you can enhance the logic further and I have answered your question.

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Jayaprakash P 

Hope you are doing good.

Did my reply answer your question?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi Ankur,

Thank you for the response, but you gave the script for current record. We are looking for script to delete for a period of time, like delete before 2023 December 31st and should we need to consider sys_attachment_doc table, when we plan to delete sys attachment/email attachment?