Last active
April 12, 2016 10:21
-
-
Save joshbirk/c73ffe8a35a90598f39a to your computer and use it in GitHub Desktop.
Simple class to try and destroy all records in a Salesforce instance. Does not currently destroy Apex, or Visualforce, or Lightning components. It probably will never do the latter, I'm not sure I want it to do the former. Does not destroy custom metadata, but I'm looking into it. TODO: Parameters to avoid limits.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DestroyAllTheThings { | |
public DestroyAllTheThings() {} | |
public void destroySimpleCRMStuff() { | |
//First clear out known dependents, leads and cases etc. | |
List<Case> cases = [SELECT ID from Case]; | |
delete cases; | |
List<Lead> leads = [SELECT ID From Lead]; | |
delete leads; | |
List<Opportunity> opportunities = [SELECT ID From Opportunity]; | |
delete opportunities; | |
List<Contact> contacts = [SELECT ID from Contact]; | |
delete contacts; | |
List<Account> accounts = [SELECT ID from Account]; | |
delete accounts; | |
} | |
public void destroyAsMuchAsPossible() { | |
//ok, now we can go and dynamically delete **everything** else | |
Map<String, Schema.SObjectType> globalDescribe = Schema.getGlobalDescribe(); | |
Set<String> sobjects = globalDescribe.keySet(); | |
for(String so : sobjects) { | |
//not allowed in DML | |
//or has other restrictions where parent/dependents need to be removed first | |
//or simply does not support query | |
//or to support our code later | |
if(globalDescribe.get(so).getDescribe().isDeletable() && globalDescribe.get(so).getDescribe().isQueryable() | |
&& so != 'EmailTemplate' && so != 'Vote' && so != 'ContentDocumentLink' | |
&& so != 'AuthSession' && so != 'ApexLog' && so != 'ObjectPermissions' && so != 'FieldPermissions' | |
&& so != 'IdeaComment') { | |
String soql = 'SELECT ID FROM '+so+' '; | |
/* ApexClass not part of this ... not deleteable/queryable from Describe? But AuthSession is? | |
if(so == 'ApexClass') { //don't delete this class | |
soql = 'SELECT ID from ApexClass where Name != \'DestroyAllTheThings\' '; | |
System.debug(soql); | |
} */ | |
List<SObject> current_objects = Database.query(soql); | |
try { | |
delete current_objects; | |
} catch (System.DmlException e) { | |
System.debug(so + ' could not be deleted'); | |
} | |
} | |
} | |
} | |
public void deleteApexAnd | |
public void reallyDestroyAllTheThings() { | |
destroySimpleCRMStuff(); | |
destroyAsMuchAsPossible(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment