need to delete files from sys_attachment table (sys attachement report)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-21-2025 03:52 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-21-2025 04:12 AM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-21-2025 04:18 AM - edited 04-21-2025 04:30 AM
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();
}
}
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-21-2025 09:17 PM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-22-2025 05:53 AM
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?