Remove duplicate rules using sfdx

duplicate-managementsalesforcedx

I need to remove the standard duplicate rules before deploying all my metadata into my scratch org. I am deploying my own duplicate rules so now the standard ones are useless. For production and other regular sandboxes I removed the standard duplicate rules manually; however, for my scratch orgs I would like to automate this. Do you know how to remove this duplicate rules? I thought it would be possible via the sfdx commands but I did not find any relevant command.

Best Answer

Try the following bash script.

First, you need to list all duplicate rules, generate destructiveChanges.xml, then use sfdx force:mdapi:deploy command to remove them.

Create a file test.sh with this content

mkdir deploy
MY_XML="deploy/destructiveChanges.xml"
rm $MY_XML
rm "deploy/package.xml"

echo '<?xml version="1.0" encoding="UTF-8"?>\n<Package xmlns="http://soap.sforce.com/2006/04/metadata">\n<version>52.0</version>\n</Package>' >> "deploy/package.xml"


echo '<?xml version="1.0" encoding="UTF-8"?>\n' >> "$MY_XML"
echo '<Package xmlns="http://soap.sforce.com/2006/04/metadata">' >> "$MY_XML"
echo '    <types>' >> "$MY_XML"
for each in $(sfdx force:mdapi:listmetadata -m DuplicateRule --json | jq '.result[].fullName' -r); do
    echo "        <members>$each</members>" >> "$MY_XML"
done
echo '        <name>DuplicateRule</name>' >> "$MY_XML"
echo '    </types>' >> "$MY_XML"
echo '    <version>52.0</version>' >> "$MY_XML"
echo '</Package>' >> "$MY_XML"
sfdx force:mdapi:deploy -d deploy -w 500

Then execute this file

./test.sh
Related Topic