- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-23-2020 06:19 AM
I'm sure I'm missing something, but it appears to me that attachments (in sys_attachment) are not deleted when the record to which they are attached is deleted.
Is there by chance a scheduled cleanup job?
Thanks,
GregM
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-03-2020 09:59 PM
Hi,
Let me know if that answered your question.
If so, please mark my response as ✅ correct & 👍 helpful so that others with the same question in the future can find it quickly and that it gets removed from the Unanswered list.
Regards
Ankur
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-23-2020 06:29 AM
Hi Greg,
I just tested this on my PDI New York release instance and the attachment record is deleted when the incident record is deleted.
- Pradeep Sharma
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-23-2020 06:31 AM
Hi,
if this is not happening out of the box then you can delete sys_attachment record once table record is deleted; going forward
Sample script below; after delete on incident example
var gr = new GlideRecord('sys_attachment');
gr.addQuery('table_sys_id', current.sys_id);
gr.query();
gr.deleteMultiple();
you will have to run one time schedule job to clean the sys_attachment table; query the table with the table_sys_id and check if record exists; if not then delete that record
deleteAttachments();
function deleteAttachments(){
try{
var attachmentRec = new GlideRecord('sys_attachment');
attachmentRec.query();
while(attachmentRec.next()){
var tableRec = new GlideRecord(attachmentRec.table_name);
tableRec.addQuery('sys_id', attachmentRec.table_sys_id);
tableRec.query();
if(!tableRec.next()){
attachmentRec.deleteRecord();
}
}
}
catch(ex){
gs.info('Exception is: ' + ex);
}
}
Mark ✅ Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur
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-23-2020 07:10 AM
Interesting. I went looking for a business rule on incident/task to see if this is what's doing it, but couldn't find anything.
I've got a custom table - should have said that, sorry.
So it appears that it's not a global effect.
GregM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-23-2020 07:57 AM
Hi,
if no BR exists then create one for deleting going forward; for cleanup you can use schedule job; sample script shared already
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader