Delete unwanted attachments from customer requests using Automation

Platform Notice: Cloud - This article applies to Atlassian products on the cloud platform.

Warning

This article uses Automation to delete attachments based on a regex. Please adjust the regex to suit your use case scenario and test it in your sandbox environment before using it in production. Deleted attachments can't be recovered.

Summary

While using the email channel, you may want to remove unwanted attachments added to the request during ticket creation or when comments are added.

Diagnosis

An email may have one or several images included in the signature, like a company or a social network logo, a think-before-printing message, etc. An email thread may have images/ logos from email signatures of multiple responders. In these cases, many attachments can build up via an email exchange making the "real" issue content hard to see, as this unnecessary content is also included in comments after email processing. 

To delete them, the user must click the  bin icon in the attachment section, wait for the confirmation screen, and click ok for each of the attachments.  However, this action won't delete the references from the comments, which will show Preview Unavailable.

Cause

We released a feature to hide email signatures from the issue view by hiding it in an expandable component: Hide email signatures from the issue view and portals. However, for some email clients, the default detection of signature does not work as expected when the signature contains multiple images, therefore after email processing these images are added as an attachment. 

Here are some open Feature requests to provide this functionality and one of them is already in Review by our Product Managers: 

Solution

Attention! Proficiency in regex, webhooks, and APIs is strongly advised for optimal results from this workaround.

This workaround works with the combinations of Webhooks and Automation, and its scope is:

  1. Remove unwanted attachments based on a Criteria (name to be defined and adapted to your business needs)
  2. Remove references to those attachments from the Issue Description (on Issue Creation)
  3. Remove references to those attachments from the Comments (on both scenarios)
  4. Not generate additional Customer notifications

How are unwanted attachments identified?

Identify the pattern in the name of the most common signature images attached to your issues. For the sake of this article, we are considering that the files we want to remove  match with this regex:

^image0\w*.+(jpg|png)$


This will match filenames that:

  • Start with  " image0 "
  • Followed by  0  or  more alphanumeric chars
  • Followed by  any kind of characters
  • Ending with  jpg or png

Edit the regex based on your use case, the regex needs to follow the syntax described below. Here are some resources you can refer to: 

Setup

The workaround involves covering 2 scenarios :

  • Remove attachments added during issue creation
  • Remove attachments added when new comments are added

Scenario 1: Remove attachments added during issue creation

(warning) Please note that the kb involves an action to delete the last comment added on the issue as soon as the issue is created since these comments refer to the attachments added during issue creation. However, if you have any other automations that are adding comments to an issue as soon as an issue is created then there are chances that such comments may get deleted. In that case, you can either choose a different trigger for the other rules or avoid using this action. If you don't add this action, and if your signature attachments are processed as comments as well, then even though attachments will be deleted, the comments will show a message  Preview Unavailable since attachments no longer exist. 

  • You can import the following rule Automation-Rule-removeAttachments-NewIssues_2023-05-17T14_49_00.478Z.json 
    • Reference article on how to import the above rule to your site - Import and export Jira automation rules
      (info) The rule is in a disabled state so that it doesn't run automatically before making the necessary changes are made to the regex that suits your needs and the necessary testing is done. Next, It's important to select the right Project Scope after importing the new rule. Also, leave the  Import rule owners unchecked. 

  • Or Create it based on the following video:


  • Or Create an automation rule with the following configuration:
    • Add trigger Field value changed  to monitor changes made to the Attachment field on issue create the action
    • Add an Advanced branching component to do certain actions on each attachment added using the smart value {{attachment.filename}} accessed with a variable name file within the branch
    • Within the branch add an Advanced compare condition to match the file name against the regular expression based on your requirement. (warning) For the scope of this kb, we are using the same expression as discussed above. If the file name matches our regular expression then add the Delete attachments action to delete the current file 
    • Outside the branch, add the component Delete comment to delete the last comment added as part of the issue creation that is referring to the attachment.
    •  Next, if these attachments are referred to in the Description field, add the Edit issue action for the Description field to replace any references to the signature file names, for this kb we are replacing references for imageXX: 

      {{issue.description.replaceAll("[\s|.]*(!image0\w.*[jpg|png]\|thumbnail!)","")}}

    • Here is what the rule will look like:

Scenario 2: New comments added including attachments

This scenario has the below requirements: 

  • Trigger a Jira webhook when an attachment is added to a specific project. This will be used for new comments including attachments.
  • Automation to delete signature attachments - Configure an automation rule that will listen for the webhook events for attachments and remove the attachments that match the signature regex
  • Automation to remove attachment references from comments

Automation to delete signature attachments

  • You can import the following rule  Automation-Rule-removeAttachments-NewComments_2023-05-17T14_12_00.217Z.json
    • (warning) After importing the rule, it's necessary to Regenerate the Webhook URL. This is done from the Trigger
    • Reference article on how to import the above rule to your site - Import and export Jira automation rules
      (info) The rule is in a disabled state so that it doesn't run automatically before making the necessary changes are made to the regex that suits your needs and the necessary testing is done. Next, It's important to select the right Project Scope after importing the new rule. Also, leave the  Import rule owners unchecked. 

  • Or, Create it based on the following video: 


  • Or Create an automation rule with the following configuration
    • Add an Incoming webhook  trigger
    • Next, add the Create variable action to store the value of smartvalue {{webhookData.attachment.filename}} in the variable file
    • Compare if the filename matches the signature regex. (warning) For the scope of this kb, we are using the same expression as discussed above. If the file name matches the regular expression then add the Delete attachments action to delete the file added

      ^image0\w*.+(jpg|png)$
  • Here is what the rule will look like:

Automation to remove attachment references from comments

  • You can import the following rule Automation-Rule-removeAttachmentsReferences4NewComments_2023-05-17T14_23_57.873Z.json
    • Reference article on how to import the above rule to your site - Import and export Jira automation rules
      (info) The rule is in a disabled state so that it doesn't run automatically before making the necessary changes are made to the regex that suits your needs and the necessary testing is done. Next, It's important to select the right Project Scope after importing the new rule. Also, leave the  Import rule owners unchecked. 

  • Or Create it based on the following video: 
  • Or, Create an automation rule with the following configuration:
    • Add trigger Issue commented for all comments 
    • Next, add a condition to check if the comment refers to any attachment using the below regex. (warning) For the scope of this kb, we are using the same expression as discussed above

      [\s|.]*(!image0\w.*[jpg|png]\|thumbnail!)
    • If the condition matches the Edit comment action to replace any references to the signature file names in the last comment. For this kb we are replacing references for imageXX: 

      {{issue.comments.last.body.replaceAll("[\s|.]*(!image0\w.*[jpg|png]\|thumbnail!)","")}}

    • Here is what the rule will look like:

Trigger Jira webhook when an attachment is added

Finally, configure a Jira webhook to be triggered when an attachment is created

  • Copy the webhook URL you have obtained from the first automation rule in this scenario
  • Go to System >  Webhooks > Create a webhook. Paste the URL and add the following variables at the end of the URL to detect the issue key 

    ?issue=${issue.key}
  • Add some JQL to limit the scope of the webhook to desired project and select the  Attachment created event. 

Last modified on Jan 22, 2025

Was this helpful?

Yes
No
Provide feedback about this article
Powered by Confluence and Scroll Viewport.