Mass deleting picklist values in Salesforce with AJAX javascript hack

Today I was gonna do a small change in our Salesforce instance, namely replacing to 1100+ picklists with brand new values. Last time I did this I used Eclipse and just saved the file (Lead.object for example).

But I realized that only new values are added and old ones are not removed and as usual there are number of requests on success.salesforce.com for this feature but it is not implemented yet.

Anyways after Googling I found this article:
http://frombelvideres4thfloor.blogspot.se/2011/05/javascript-hack-to-mass-delete-custom.html
which basically solves the problem by opening new windows.

This seemed to put a lot of stress on my poor MacBook so I edited it to use synchronous AJAX requests instead.


/* Gather all of the a elements on the page. */

var links = document.getElementsByTagName("a");

/* Pick out the Del links. */

var delLinks = new Array();
for (var i = 0; i < links.length; i++) {
  var link = links[i];

  if (link.innerHTML == "Del") {
    /*alert("Del link found!");*/
    /*alert(link.attributes['href'].value);*/
    delLinks[delLinks.length] = link;
  }
}

/* AJAX request each Del link to delete the associated
   picklist value.

   This code can be augmented as desired
   to only delete certain values.

   However, for custom picklists it's probably
   easier to just delete all of the values
   and then re-add the desired values. */

for (var i = 0; i < delLinks.length; i++) {
  var delLink = delLinks[i];
  // Synchronous AJAX style
  xmlhttp = new XMLHttpRequest();
  xmlhttp.open("GET",delLink.attributes['href'].value,false);
  xmlhttp.send();
}

The last 3 lines are what I’ve changed and it will call all delete links on a picklist configuration page. To get it running just create a new bookmark in your browser.

javascript:(function()%7Bvar s = document.createElement("script"); s.src = "https://superfredag.com/massdelete.js"; void(document.body.appendChild(s));%7D)()

And replace the url with where you put your javascript file, or use mine if you want to.

Then just go to a picklist edit page and click it, it will take a while to execute but you can see that things are happening by opening Developer Tools (I’m using Chromium) and go to the Network tab.

Use it with caution though, it worked for me but you never know when Salesforce changes the layout. Test in your staging environment first 😉

This helped me remove 1100+ picklist values with very little effort.

Big thanks to  for writing the original post.

12 thoughts on “Mass deleting picklist values in Salesforce with AJAX javascript hack

  1. You are a life saver!!!! I had 845 picklist values to delete. It would have taken me FOREVER to do each one!

    Thanks for posting!!

  2. Hi. I added the script to my server and also tried calling yours, but no luck in either case. Is there anything I could be missing? Thanks.

  3. Noticing that now when I open delete in a new tab I’m prompted for a replacement value. Could this be the issue? Is there a workaround for it that you’ve come up with yet?

    • Hey Jem,

      Sorry for being slow in responding.

      I used the script recently I think but I don’t remember any problems, is it a picklist that is mandatory so you need to set a new value for the deleted ones?

  4. Hi Johan, any update regarding this hack? Receiving message now that Synchronous XMLHttpRequest has been deprecated.

    • Hej Robin,

      Haven’t looked into fixing that but I guess I could change it to using asynchronous requests and a callback.
      Thanks for letting me know, will reply when I have fixed it!

  5. Pingback: Mass Delete Picklist values | DL-UAT

  6. Here’s a variant I used to make the XMLHttpRequests async.

    It spits out a lot of errors to the javascript console, but it does work.

    Note my additional criteria …. I wanted to retain values that contained United States or Canada.

    var links = document.getElementsByTagName(“a”);

    /* Pick out the Del links. */

    var delLinks = new Array();

    for (var i = 0; i < links.length; i++) {
    var link = links[i];
    if(link.innerHTML === 'Del' && link.title.indexOf('United States') === -1 && link.title.indexOf('Canada') === -1){
    delLinks.push(link);
    }
    }

    for (var i = 0; i < delLinks.length; i++) {
    var delLink = delLinks[i];
    // Synchronous AJAX style
    xhr = new XMLHttpRequest();
    xhr.open("GET",delLink.attributes['href'].value,true);
    xhr.onload = function (e) {
    if (xhr.readyState === 4) {
    if (xhr.status === 200) {
    console.log(xhr.responseText);
    } else {
    console.error(xhr.statusText);
    }
    }
    };
    xhr.onerror = function (e) {
    console.error(xhr.statusText);
    };
    xhr.send(null);
    }

  7. Pingback: Salesforce: How to mass delete picklist values? | Klomos

  8. Hey,

    I just tried this today but it is not working . I remember trying this a year ago, It worked then but not now. Any Idea?

    • Hi Mahaboob,

      I haven’t used the script for years now but I will try to start blogging again and hopefully I will have time to look at this.

      Cheers,
      Johan

Leave a Reply

Your email address will not be published. Required fields are marked *