Clean up of attachments

gregm3
Giga Contributor

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

1 ACCEPTED SOLUTION

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

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

View solution in original post

5 REPLIES 5

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

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

Ankur Bawiskar
Tera Patron
Tera Patron

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

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

gregm3
Giga Contributor

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

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

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