Delete from Attachment table using Script

Abhi369
Tera Contributor

I'm deleting a record from sys_attachment table using below background script but getting Row count as 2. It is always double the number of records deleted from sys_attachment table.

 

var gr= new GlideRecord('sys_attachment');
gr.addQuery('sys_id','9d008fb29766319331f053af69');

gr.query();
if(gr.next())
{
gr.setWorkflow(false);
gr.deleteRecord();
}

 

Just wanted to know why is it like this and if there is any risk or anything extra getting deleted. Just wanted to be sure before executing this background script in the production.

 

Attached the Screenshot as well for reference.

 

Thank you!

4 REPLIES 4

chetanb
Tera Guru

Hello @Abhi369 ,

 

Use fixscript to delete attachment from sys_attachment table.

I would suggest create  db view in dev env for the tables which involved in the process.

For eg- for incident attachment -create db view for incident and sys_attachment table.

 

If you provide business usecase, I will guide with efficient solution.

 

if my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.

 

Regards,

CB

Abhi369
Tera Contributor

Hi Chetan,

 

I checked and I know that I'm able to delete that attachment from sys_attachment table as well as from the closed incident. But I want to be sure that I'm not deleting anything extra.

Is it normal that we get Row Count as 2 when we delete single record from sys_attachment table?

 

Thank you,

Abhi

Hello @Abhi369 ,

 

This could be because gliderecord query returning both parent and child attachments associated with with the specified sys_id.

 

if my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.

 

Regards,

CB

Arunkumar_ganes
Tera Contributor

@Abhi369 Yes, thats expected behaviour as deleting sys_attachment record will do cascade deltion of sys_attachment_doc records which contains actual attached file.