- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
21 hours ago
Hi Team,
I have the below scenario to be implemented help here please.
When a record from the Knowledge Base (kb_knowledge) table is deleted, you want to delete all its attachments to save space.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
21 hours ago
The Below Simple code will help you !!
// Table: kb_knowledge
// When: before delete
(function executeRule(current, previous) {
var attachmentGR = new GlideRecord('sys_attachment');
attachmentGR.addQuery('table_name', 'kb_knowledge');
attachmentGR.addQuery('table_sys_id', current.sys_id);
attachmentGR.query();
while (attachmentGR.next()) {
attachmentGR.deleteRecord();
}
})(current, previous);
If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!
Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI
YouTube: https://www.youtube.com/@learnservicenowwithravi
LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
20 hours ago
kb_knowledge is the table for knowledge articles not knowledge base.
Try this script in After Delete Business rule on kb_knowledge table:
var att = new GlideRecord('sys_attachment');
att.addEncodedQuery('table_sys_id=' + current.sys_id);
att.query();
att.deleteMultiple();
Please mark the answer correct/helpful accordingly.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
20 hours ago
Hi @vidishaagarwal5 ,
1) on you table you can create the after business rule (delete)
and paste this script in your business rule
(function executeRule(current, previous /*null when async*/) {
//thiss Find all attachments linked to this Knowledge record
var attachmentGR = new GlideRecord('sys_attachment');
attachmentGR.addQuery('table_sys_id', current.sys_id);
attachmentGR.addQuery('table_name', 'kb_knowledge');
attachmentGR.query();
//now Delete each attachment
while (attachmentGR.next()) {
attachmentGR.deleteRecord();
}
})(current, previous);
If my response/article helped you, please mark it as the correct answer and close the thread — this helps other readers in the community.
Regards,
Tejas
🚀 ServiceNow Developer | 🏆 HackaNow Finalist | 💡 Community Contributor
📧 Email: tejas.adhalrao11@gmail.com
🔗 LinkedIn: linkedin.com/in/tejas1018
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
20 hours ago - last edited 20 hours ago
@vidishaagarwal5,Once you delete an article record, no attachment will appear in the sys_attachment table, so they are removed automatically from there but might still remain part of the system. Follow this thread to remove them permanently.
Muhammad Iftikhar
If my response helped, please mark it as the accepted solution so others can benefit as well.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
21 hours ago
The Below Simple code will help you !!
// Table: kb_knowledge
// When: before delete
(function executeRule(current, previous) {
var attachmentGR = new GlideRecord('sys_attachment');
attachmentGR.addQuery('table_name', 'kb_knowledge');
attachmentGR.addQuery('table_sys_id', current.sys_id);
attachmentGR.query();
while (attachmentGR.next()) {
attachmentGR.deleteRecord();
}
})(current, previous);
If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!
Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI
YouTube: https://www.youtube.com/@learnservicenowwithravi
LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
21 hours ago
Let me try this one .. I will get back to you
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
20 hours ago
kb_knowledge is the table for knowledge articles not knowledge base.
Try this script in After Delete Business rule on kb_knowledge table:
var att = new GlideRecord('sys_attachment');
att.addEncodedQuery('table_sys_id=' + current.sys_id);
att.query();
att.deleteMultiple();
Please mark the answer correct/helpful accordingly.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
20 hours ago
Hi @vidishaagarwal5 ,
1) on you table you can create the after business rule (delete)
and paste this script in your business rule
(function executeRule(current, previous /*null when async*/) {
//thiss Find all attachments linked to this Knowledge record
var attachmentGR = new GlideRecord('sys_attachment');
attachmentGR.addQuery('table_sys_id', current.sys_id);
attachmentGR.addQuery('table_name', 'kb_knowledge');
attachmentGR.query();
//now Delete each attachment
while (attachmentGR.next()) {
attachmentGR.deleteRecord();
}
})(current, previous);
If my response/article helped you, please mark it as the correct answer and close the thread — this helps other readers in the community.
Regards,
Tejas
🚀 ServiceNow Developer | 🏆 HackaNow Finalist | 💡 Community Contributor
📧 Email: tejas.adhalrao11@gmail.com
🔗 LinkedIn: linkedin.com/in/tejas1018