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:
- 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.
- 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
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
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.