Remove Salesforce for Outlook Signature Images

Automate the Monitoring of Salesforce Trust
February 4, 2016
How to Setup My Email to Salesforce
September 21, 2016

Remove Salesforce for Outlook Signature Images

Applies to: Lightning and Classic

Salesforce for Outlook and now Lightning for Outlook are great tools for adding emails into Salesforce.  However, many of our emails have signature lines that include logos or other images that clog up storage and add no value.  Below is how I eliminate these unnecessary images.

There’s a lot of code snippets to making this happen but you should be able to cut and paste it.  I’ll walk you through the process.

First, create a new class and name it “GlobalHelper” (or choose your own name and use it throughout the code).  This is the main piece of code that finds and deletes the images.  Paste in the following:

&amp;lt;br /&amp;gt;global class GlobalHelper {&amp;lt;br /&amp;gt;global static void deleteImageAttachments(){&amp;lt;br /&amp;gt;// removed the &amp;quot;image&amp;quot; files from Attachments. These are almost always images from email signature lines&amp;lt;br /&amp;gt;// no magic to the LIMITs on the SQOL, just preventing the possibility of governor limits&amp;lt;/p&amp;gt;<br>
&amp;lt;p&amp;gt;list&amp;amp;amp;lt;Attachment&amp;amp;amp;gt; aDel = new list&amp;amp;amp;lt;Attachment&amp;amp;amp;gt;();&amp;lt;br /&amp;gt;for (Attachment a : [SELECT Id, ParentId FROM Attachment WHERE Name LIKE 'image%' LIMIT 10000]){&amp;lt;br /&amp;gt;if ( ((string)a.ParentId).substring(0,3) &amp;amp;amp;lt;&amp;amp;amp;gt; '02s') aDel.add(a);&amp;lt;br /&amp;gt;}&amp;lt;br /&amp;gt;if (!aDel.isEmpty()) delete aDel;&amp;lt;br /&amp;gt;}&amp;lt;br /&amp;gt;}&amp;lt;br /&amp;gt;

Next, we want to schedule this code to run nightly so we need to create a schedulable class. So lets create another new class and call it “schedulableDeleteImageAttachments”. Its code will look like this:

&amp;lt;br /&amp;gt;public class schedulableDeleteImageAttachments implements Schedulable{&amp;lt;br /&amp;gt;public void execute(SchedulableContext Sche){&amp;lt;br /&amp;gt;GlobalHelper.deleteImageAttachments();&amp;lt;br /&amp;gt;}&amp;lt;br /&amp;gt;}&amp;lt;br /&amp;gt;

You’ll need test classes for these so lets create those now. Create a class called “GlobalHelperTest”. Its code will look like:

&amp;lt;br /&amp;gt;@isTest&amp;lt;br /&amp;gt;private class GlobalHelperTest {&amp;lt;br /&amp;gt;public static testMethod void testDeleteImageAttachments(){&amp;lt;br /&amp;gt;Account a = new Account();&amp;lt;br /&amp;gt;insert a;&amp;lt;/p&amp;gt;<br>
&amp;lt;p&amp;gt;Attachment at = new Attachment(name='image01', ParentId=a.Id, body=Blob.valueof('myBlob'));&amp;lt;br /&amp;gt;insert at;&amp;lt;/p&amp;gt;<br>
&amp;lt;p&amp;gt;GlobalHelper.deleteImageAttachments();&amp;lt;br /&amp;gt;}&amp;lt;br /&amp;gt;}&amp;lt;br /&amp;gt;

Create another test class for the schedulable class called “schedulableDeleteImageAttachmentsTest” with the following code:

@isTest&amp;lt;br /&amp;gt;public class schedulableDeleteImageAttachmentsTest {&amp;lt;br /&amp;gt;static testMethod void myUnitTest() {&amp;lt;br /&amp;gt;for (CronTrigger c : [SELECT Id, CronJobDetailId, CronJobDetail.JobType, CronJobDetail.Name, State&amp;lt;br /&amp;gt;FROM CronTrigger WHERE CronJobDetail.JobType = '7' AND CronJobDetail.Name = 'testDeleteImage']){&amp;lt;br /&amp;gt;system.abortJob(c.Id);&amp;lt;br /&amp;gt;}&amp;lt;/p&amp;gt;<br>
&amp;lt;p&amp;gt;test.starttest();&amp;lt;br /&amp;gt;schedulableDeleteImageAttachments dia = new schedulableDeleteImageAttachments();&amp;lt;br /&amp;gt;String sch8 = '0 00 3 * * ?';&amp;lt;br /&amp;gt;system.schedule('testDeleteImage', sch8, dia);&amp;lt;br /&amp;gt;test.stopTest();&amp;lt;/p&amp;gt;<br>
&amp;lt;p&amp;gt;for (CronTrigger c : [SELECT Id, CronJobDetailId, CronJobDetail.JobType, CronJobDetail.Name, State&amp;lt;br /&amp;gt;FROM CronTrigger WHERE CronJobDetail.JobType = '7' AND CronJobDetail.Name = 'testDeleteImage']){&amp;lt;br /&amp;gt;system.abortJob(c.Id);&amp;lt;br /&amp;gt;}&amp;lt;br /&amp;gt;}&amp;lt;br /&amp;gt;}

That should take care of the code requirements.  So now we need to setup the schedule within Salesforce.

  • Using Classic, navigate to Setup and search setup for “Apex Classes”.
  • In the body of the page, click the button called “Schedule Apex”
  • Give the job a meaningful name like “Delete Email Images”
  • Select the Apex class called “schedulableDeleteImageAttachments”
  • Choose your preferred schedule
    • Note: you’ll want to manually type in the End time so you can enter a year that is out into the future.
  • Save the scheduled job.

That should do it for you.  The email images will now be automatically deleted when your scheduled job runs.

Hope this is helpful for you!  If so, leave me a comment and let me know.

Terry Miller
Terry Miller
Terry has spent over 20 years focused on business leadership and information technology. As an independent consultant, he enjoys working with a variety of customers to help them solve business problems using the Salesforce® platform. His ability to quickly identify bottlenecks and provide understandable solutions has gained him the trust of his customers. If you're looking for expert guidance on your next Salesforce® project, click here to contact Terry today.


  1. rafy zhao says:

    Hi Terry, thank you for this great post. It helps me a lot. For one thing, if the ParentId starts with ‘0s2’, then it is for an email message. So, shall it be ‘if ( ((string)a.ParentId).substring(0,3) == ’02s’) aDel.add(a);’?

    • Terry Miller says:

      Thank Rafy. Are you asking why am I looking only at ParentId = “0s2”? I do that to prevent images that might be added to non-email objects from being deleted. For example, if an image is updated to an account object, I don’t want to inadvertently delete that.

  2. rafy says:

    Yes. You want to delete the images only coming from emails. So, I think

    if ( ((string)a.ParentId).substring(0,3) ’02s’) aDel.add(a);

    should be

    if ( ((string)a.ParentId).substring(0,3) == ’02s’) aDel.add(a);

    am I right?

  3. easeus data recovery 11.9 crack says:

    Hi, here from bing, i enjoyng this, i will come back soon.

Leave a Reply