So I am testing out the Trailhead module App Development with Salesforce DX. As a part of this, I need to import data from a local JSON file into the scratch org. The Salesforce DX CLI command is

sfdx force:data:tree:import -f data/Account.json -u GeoTestOrg

Question: If I execute this n-times, the same data in the JSON is imported n-times.

Does Salesforce DX do any duplicate checks?

  • 2
    Unless you provide unique identifiers, duplicates are expected.
    – sfdcfox
    Nov 15, 2017 at 18:34
  • that's what i was thinking..
    – OK999
    Nov 15, 2017 at 18:36

1 Answer 1


The underlying API that the Salesforce DX CLI uses in order to import is the SObject Tree API. This is an insert-only API. So if you run the job multiple times, then yes, you'll have lots of duplicates.

Presumably if you wanted some logic to reset the orgs data to the original state, you'd have to delete the data, then run the import job again. Or, this being Salesforce DX, delete your scratch org and start over again. Scratch orgs are designed to be disposable.

You do have an alternative of course: create a script that will clean up your data from Salesforce DX CLI. I looked into two options:

  1. Use the output record IDs to generate CSVs that you then pass into the sfdx force:data:bulk:delete command to do cleanup from the CLI.
  2. Run an anonymous apex script from the CLI that does the same.

After some exploration, I think the second is the simplest.

Take the example of the Dreamhouse app. When I run the tree import command I get the following output.

REFERENCE ID           TYPE         ID
─────────────────────  ───────────  ──────────────────
CarolineBrookerRef     Broker__c    a019E000003cNEKQA2
MichaelJonesRef        Broker__c    a019E000003cNELQA2
JonathanBradleyRef     Broker__c    a019E000003cNEMQA2
JenniferWuRef          Broker__c    a019E000003cNENQA2
OliviaGreenRef         Broker__c    a019E000003cNEOQA2
MiriamAupontRef        Broker__c    a019E000003cNEPQA2
MichelleLambertRef     Broker__c    a019E000003cNEQQA2
SeniorBrokerRef        Broker__c    a019E000003cNERQA2
18HenryStRef           Property__c  a049E000003uSWCQA2
24PearlStRef           Property__c  a049E000003uSWDQA2
72FrancisStRef         Property__c  a049E000003uSWEQA2
32PrinceStRef          Property__c  a049E000003uSWFQA2
110BaxterStRef         Property__c  a049E000003uSWGQA2
448HanoverStRef        Property__c  a049E000003uSWHQA2
127EndicottStRef       Property__c  a049E000003uSWIQA2
48BrattleStRef         Property__c  a049E000003uSWJQA2
121HarborwalkRef       Property__c  a049E000003uSWKQA2
640HarrisonAveRef      Property__c  a049E000003uSWLQA2
95GloucesterStRef      Property__c  a049E000003uSWMQA2
145CommonwealthAveRef  Property__c  a049E000003uSWNQA2

This tells me I need to delete all the Property__c and Broker__c records.

So I create a file my-apex-script.txt that looks like this:

delete [SELECT Id FROM Broker__c];
delete [SELECT Id FROM Property__c];

Then I simply run the following:

$ sfdx force:apex:execute -f my-apex-script.txt

Compiled successfully.
Executed successfully.

I'm now all set to rerun my original data tree import job to reset the data to the start state, thus preventing duplicates from multiple imports.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .