I am migrating my changes from one org to another. But when I have retrieved an XML file containing custom fields I wants to remove some fields from that file as I would like to remove it from org as well. So for example:-

Lets say I have following paragraph in XML files and I want to remove this paragraph from other files as well but unfortunately the tag table is not constant it's changing in differwnt files which is the problem. Can anyone please suggest me how I can do this.


enter image description here

  • When you say other files, which files are you referring to? – Jayant Das May 9 '18 at 12:35
  • 4
    Removing a custom field from component source XML doesn't remove the custom field from a target org if it already exists there. You need to use destructiveChanges.xml to remove existing components. – David Reed May 9 '18 at 12:50

Try using sed. I had a similar need specifically to remove the following section from Admin.profile-meta.xml every time I retrieved source from one of our shared sandboxes.


To do this I put the following sed command in a Bash script, and it works on macOS High Sierra 10.13.6 (17G3025).

# clean.sh scrubs the metadata to make it shiny and friendly

# Get rid of the ManageSandboxes user permission which shows up
# in metadata retrieved from Staging but is not compatible with Developer
# sandboxes
sed -E -i "" -e '/<userPermissions>/{

    # Expect the enabled element

    # Expect the name element

    # Expect the closing userPermissions tag

    # Flag the ManageSandboxes permission

    # Skip printing these lines to effectively
    # delete the unwanted metadata
    t found

    # Print to preserve the pattern space
    # for all other metadata to keep


    # Delete the pattern space and move on
}' force-app/main/default/profiles/Admin.profile-meta.xml

A Salesforce DX plugin to handle this would be nice, but as I was poking around with creating a plugin and toying with a configurable .justxml config file similar in concept to .forceignore, going that route would've taken me more time than was available due to lack of familiarity with TypeScript.

| improve this answer | |

I've recently started using a Python script for doing these kinds of XML manipulations, and I haven't been looking back ever since! With the appropriate dependencies (ie. Python + lxml module), I can run the script as part of a Bitbucket Pipeline or could probably pretty easily run it in a CLI plugin (in a bash window, I just run the following command: python build/deploy.py).

Here's what it looks like in Python using Marty's example:

from lxml import etree as ET
import os, shutil
import sys

NS = {"sf": "http://soap.sforce.com/2006/04/metadata"}
parser = ET.XMLParser(remove_blank_text=True)

# Loop through all files in profiles folder
for filename in os.listdir("./src/profiles"):

    # Parse XML file
    fxml = ET.parse("./src/profiles/" + filename, parser)

    # Look for any "name" node with text value "ManageSandboxes" 
    for elem in fxml.xpath("//sf:name[text()='ManageSandboxes']/.." , namespaces=NS):

        # Remove its parent

    # Write back to the XML file
    fxml.write("./src/profiles/" + filename, pretty_print=True, encoding='UTF-8', xml_declaration=True)

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.