Deleting Attachment Programmatically in ServiceNow
- nathanlee142
- May 12
- 4 min read
Updated: May 13

Attachments in ServiceNow can be incredibly useful for providing context and supporting information for various records. However, there are times when you need to programmatically remove these attachments. Whether it's part of a data cleanup process, a specific business rule, or a custom workflow, understanding how to delete attachments using scripting is a valuable skill for any ServiceNow developer. This article will guide you through the methods available within the GlideSysAttachment class to effectively manage and remove attachments from your ServiceNow instances.
Why Programmatically Remove Attachments in ServiceNow?
While users can manually delete attachments from records, there are many scenarios where programmatic removal becomes necessary. This could involve automatically deleting temporary files after processing, removing outdated documentation during record updates, or managing attachments as part of a complex workflow. ServiceNow provides the GlideSysAttachment class to handle such operations, allowing developers to control attachments through scripting.
Effective Methods for Deleting Attachments with GlideSysAttachment
The GlideSysAttachment class offers several ways to remove attachments, depending on your specific needs. Let's explore the most common and effective methods.
1. Removing All Attachments from a Record: The deleteAll() Method
If your requirement is to remove all attachments associated with a specific ServiceNow record, the deleteAll() method is the most straightforward solution. This method takes a GlideRecord object as an argument, representing the record from which you want to delete all attachments.
Here's how you can use it:
// Assuming 'incidentGR' is a GlideRecord object representing an Incident record
var attach = new GlideSysAttachment();
attach.deleteAll(incidentGR);
// All attachments associated with the Incident record will be deleted.
Explanation:
First, you instantiate a new GlideSysAttachment object.
Then, you call the deleteAll() method, passing in the GlideRecord object of the record whose attachments you want to remove.
ServiceNow then handles the process of identifying and deleting all attachments linked to that record.
Example Use Case
Imagine a workflow where, upon resolving an Incident, all related attachments that are older than 30 days should be automatically removed to save storage space. You could use a Business Rule that triggers on Incident resolution and uses deleteAll() after querying for the relevant Incidents.
2. Removing Specific Attachments: Using getAllAttachments() and deleteAttachment()
For scenarios where you need more granular control and want to delete specific attachments based on certain criteria (like filename or upload date), you can use a combination of getAllAttachments() and deleteAttachment().
Here's the process:
// Assuming 'taskSysId' is the sys_id of a Task record
var attachment = new GlideSysAttachment();
var attachmentGR = attachment.getAllAttachments('task', taskSysId); // 'task' is the table name
while (attachmentGR.next()) {
// You can add conditions here to target specific attachments
if (attachmentGR.getValue('file_name').indexOf('old_document') > -1) {
attachment.deleteAttachment(attachmentGR.getValue('sys_id'));
// This specific attachment will be deleted.
}
}
Explanation:
You create a GlideSysAttachment object.
getAllAttachments(tableName, tableSysId) returns a GlideRecord object containing all attachments for the specified table and sys_id.
You then iterate through this GlideRecord of attachments.
Inside the loop, you can add conditions to identify the specific attachments you want to remove (e.g., checking the filename).
Finally, deleteAttachment(attachmentSysId) deletes the attachment with the specified sys_id.
Example Use Case:
Consider a process where users might upload multiple versions of a document. You might want to create a scheduled job that periodically checks for older versions (based on filename conventions) and deletes them using this method, keeping only the latest version attached.
3. Considerations for Scoped Applications
If you are working within a scoped application, it's important to note that the behavior of GlideSysAttachment might differ slightly from the global scope. As highlighted in the community discussion, the deleteAll() method might not be directly available in scoped applications in older ServiceNow versions.
In such cases, a common workaround is to query the sys_attachment table directly within your scope and then use the deleteAttachment() method of the scoped GlideSysAttachment class.
Here's an example for scoped applications:
function deleteAllAttachmentsScoped(record) {
var gsa = new GlideSysAttachment();
var attachmentGR = new GlideRecord('sys_attachment');
attachmentGR.addQuery('table_name', record.getTableName());
attachmentGR.addQuery("table_sys_id", record.sys_id);
attachmentGR.query();
while (attachmentGR.next()) {
gsa.deleteAttachment(attachmentGR.getValue('sys_id'));
}
}
// Assuming 'incidentGR' is a GlideRecord object
deleteAllAttachmentsScoped(incidentGR);
Explanation:
This function takes a GlideRecord object as input.
It instantiates the scoped GlideSysAttachment class.
It then directly queries the sys_attachment table to find attachments related to the given record.
Finally, it iterates through the found attachments and uses the scoped deleteAttachment() method to remove each one.
This approach ensures that you are respecting the scope and application access policies within your ServiceNow instance.
Conclusion: Choosing the Right Method for Attachment Removal
The GlideSysAttachment class provides powerful tools for managing attachments in ServiceNow. For removing all attachments from a record, the deleteAll() method is efficient and straightforward. When you need more control over which attachments to delete, using getAllAttachments() in conjunction with deleteAttachment() allows for targeted removal based on specific criteria. Developers working in scoped applications should be mindful of potential differences and might need to use the alternative approach of querying sys_attachment directly. By understanding these methods, you can effectively automate and manage attachments within your ServiceNow solutions, ensuring data integrity and efficient storage utilization. Remember to always test your scripts thoroughly in a non-production environment before deploying them to your live instance.