How to remove file attachments using the SDK

I am looking for ways to programmatically remove file attachments from work items.  If I recall correctly, the system only keeps the file for as long as the relationship exists, so that, in theory, the file is deleted from the database a soon as the relationship is removed.

I found this Using the SDK to Create and Edit Objects and Relationships Using Type Projections

Is this the only way or are the other, simpler ways for deleting relationships and/or file attachments?
Is this something I could do with the Orchestrator Remove Relationship activity?

A little background on this question: I have multiple different teams with varying data retention requirements for Service Requests.  My longest requirement is 3 years (audit) while most teams only require 90 days.  I was looking to add in some jobs for some more aggressive file attachment grooming to keep the database smaller.

Thanks.

January 30th, 2015 10:57pm

The relationship is defined as a membership/containment relationship. So, yes, if you delete the relationship, the file attachment (and the associated blob) are deleted as well.

Based on the background you described, I'm assuming you identify a bunch of work orders whose files you want to delete if they're older than X. So, for instance "Work Items with Y team; delete file attachments older than 90 days", "Work Items with Q team; delete file attachments older than 3 years". Is that right?

If so, you don't necessarily need to delete relationships specifically...you can simply delete the file attachment objects which will delete the relationship, the file attachment object, and the associated blob (the file itself in the database).

Using the SDK, you will not need type projections for this. You'll only need the work item object(s) (an incident or service request or whatever), the GetRelatedObjects<>() method, and an IncrementalDiscoveryData object.

So, after you get all of the appropriate work items, you can use the following snippet to delete their file attachments if the attachments meet your age requirements

//Connect to the management group
String strMySCSMServer = "<my mgmt server>";
emg = new EnterpriseManagementGroup(strMySCSMServer);

IncrementalDiscoveryData idd = new IncrementalDiscoveryData();
ManagementPackRelationship relWorkItemHasFileAttachment = emg.EntityTypes.GetRelationshipClass(new Guid("AA8C26DC-3A12-5F88-D9C7-753E5A8A55B4")); //System.WorkItemHasFileAttachment

//Get the work item's related file attachments using it's Id
Guid myWorkItemGuid = new Guid("<some work item guid>");
IList<EnterpriseManagementObject> lstFileAttachments = emg.EntityObjects.GetRelatedObjects<EnterpriseManagementObject>(myWorkItemGuid, relWorkItemHasFileAttachment, TraversalDepth.OneLevel, ObjectQueryOptions.Default);

//Loop through each file attachment
foreach (EnterpriseManagementObject emoFile in lstFileAttachments)
{
    //Determine its age
    DateTime AddedDate = (DateTime)emoFile[null, "AddedDate"].Value;
    TimeSpan FileAttachmentAge = AddedDate.Subtract(DateTime.Now);
    //Prep the file attachment for deletion if it's old enough, in this example, older than 90 days
    if (FileAttachmentAge.Days > 90)
        idd.Remove(emoFile);
}
//Submit the deletions to the database.
idd.Commit(emg);

  • Marked as answer by Foothill1 Friday, January 30, 2015 9:50 PM
Free Windows Admin Tool Kit Click here and download it now
January 31st, 2015 12:04am

The relationship is defined as a membership/containment relationship. So, yes, if you delete the relationship, the file attachment (and the associated blob) are deleted as well.

Based on the background you described, I'm assuming you identify a bunch of work orders whose files you want to delete if they're older than X. So, for instance "Work Items with Y team; delete file attachments older than 90 days", "Work Items with Q team; delete file attachments older than 3 years". Is that right?

If so, you don't necessarily need to delete relationships specifically...you can simply delete the file attachment objects which will delete the relationship, the file attachment object, and the associated blob (the file itself in the database).

Using the SDK, you will not need type projections for this. You'll only need the work item object(s) (an incident or service request or whatever), the GetRelatedObjects<>() method, and an IncrementalDiscoveryData object.

So, after you get all of the appropriate work items, you can use the following snippet to delete their file attachments if the attachments meet your age requirements

//Connect to the management group
String strMySCSMServer = "<my mgmt server>";
emg = new EnterpriseManagementGroup(strMySCSMServer);

IncrementalDiscoveryData idd = new IncrementalDiscoveryData();
ManagementPackRelationship relWorkItemHasFileAttachment = emg.EntityTypes.GetRelationshipClass(new Guid("AA8C26DC-3A12-5F88-D9C7-753E5A8A55B4")); //System.WorkItemHasFileAttachment

//Get the work item's related file attachments using it's Id
Guid myWorkItemGuid = new Guid("<some work item guid>");
IList<EnterpriseManagementObject> lstFileAttachments = emg.EntityObjects.GetRelatedObjects<EnterpriseManagementObject>(myWorkItemGuid, relWorkItemHasFileAttachment, TraversalDepth.OneLevel, ObjectQueryOptions.Default);

//Loop through each file attachment
foreach (EnterpriseManagementObject emoFile in lstFileAttachments)
{
    //Determine its age
    DateTime AddedDate = (DateTime)emoFile[null, "AddedDate"].Value;
    TimeSpan FileAttachmentAge = AddedDate.Subtract(DateTime.Now);
    //Prep the file attachment for deletion if it's old enough, in this example, older than 90 days
    if (FileAttachmentAge.Days > 90)
        idd.Remove(emoFile);
}
//Submit the deletions to the database.
idd.Commit(emg);

  • Marked as answer by Foothill1 Friday, January 30, 2015 9:50 PM
January 31st, 2015 12:04am

The relationship is defined as a membership/containment relationship. So, yes, if you delete the relationship, the file attachment (and the associated blob) are deleted as well.

Based on the background you described, I'm assuming you identify a bunch of work orders whose files you want to delete if they're older than X. So, for instance "Work Items with Y team; delete file attachments older than 90 days", "Work Items with Q team; delete file attachments older than 3 years". Is that right?

If so, you don't necessarily need to delete relationships specifically...you can simply delete the file attachment objects which will delete the relationship, the file attachment object, and the associated blob (the file itself in the database).

Using the SDK, you will not need type projections for this. You'll only need the work item object(s) (an incident or service request or whatever), the GetRelatedObjects<>() method, and an IncrementalDiscoveryData object.

So, after you get all of the appropriate work items, you can use the following snippet to delete their file attachments if the attachments meet your age requirements

//Connect to the management group
String strMySCSMServer = "<my mgmt server>";
emg = new EnterpriseManagementGroup(strMySCSMServer);

IncrementalDiscoveryData idd = new IncrementalDiscoveryData();
ManagementPackRelationship relWorkItemHasFileAttachment = emg.EntityTypes.GetRelationshipClass(new Guid("AA8C26DC-3A12-5F88-D9C7-753E5A8A55B4")); //System.WorkItemHasFileAttachment

//Get the work item's related file attachments using it's Id
Guid myWorkItemGuid = new Guid("<some work item guid>");
IList<EnterpriseManagementObject> lstFileAttachments = emg.EntityObjects.GetRelatedObjects<EnterpriseManagementObject>(myWorkItemGuid, relWorkItemHasFileAttachment, TraversalDepth.OneLevel, ObjectQueryOptions.Default);

//Loop through each file attachment
foreach (EnterpriseManagementObject emoFile in lstFileAttachments)
{
    //Determine its age
    DateTime AddedDate = (DateTime)emoFile[null, "AddedDate"].Value;
    TimeSpan FileAttachmentAge = AddedDate.Subtract(DateTime.Now);
    //Prep the file attachment for deletion if it's old enough, in this example, older than 90 days
    if (FileAttachmentAge.Days > 90)
        idd.Remove(emoFile);
}
//Submit the deletions to the database.
idd.Commit(emg);

  • Marked as answer by Foothill1 Friday, January 30, 2015 9:50 PM
Free Windows Admin Tool Kit Click here and download it now
January 31st, 2015 12:04am

This looks to be exactly what I am looking for.  Thanks.

An other note, could I do something with the Service Requests themselves?  I have the system data retention set to 3 years, could I implement targeted grooming using a similar practice?

  • Edited by Foothill1 Friday, January 30, 2015 9:55 PM Requesting additional information.
January 31st, 2015 12:50am

This looks to be exactly what I am looking for.  Thanks.

An other note, could I do something with the Service Requests themselves?  I have the system data retention set to 3 years, could I implement targeted grooming using a similar practice?

  • Edited by Foothill1 Friday, January 30, 2015 9:55 PM Requesting additional information.
Free Windows Admin Tool Kit Click here and download it now
January 31st, 2015 12:50am

This looks to be exactly what I am looking for.  Thanks.

An other note, could I do something with the Service Requests themselves?  I have the system data retention set to 3 years, could I implement targeted grooming using a similar practice?

  • Edited by Foothill1 Friday, January 30, 2015 9:55 PM Requesting additional information.
January 31st, 2015 12:50am

Of course. Just put your service request objects into the IDD with the Remove() method and commit() it.

Make sure you clearly understand what you're removing, though. Using the IDD's "Remove()" method forcibly removes the object from Service Manager. In other words, it has nothing to do with "closing" work items or setting CIs to "pending delete". It tells SCSM "this object no longer exists. Set it to IsDeleted"..two days later, it's completely removed from SQL

I should point out that removing work items yourself goes against ITIL principals and SCSM's implementation of those principals.

Free Windows Admin Tool Kit Click here and download it now
January 31st, 2015 1:46am

Ah, I see that while I can remove them, it's deleting and not grooming.  I wonder if there are ways to have a much more robust grooming configuration which allow for variable grooming by criteria instead of a single number applied to all of objects of a class.
February 2nd, 2015 10:02am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics