+ Start a Discussion
Andrew84Andrew84 

Delete records arbitrarily by id

Is there a way to delete records with apex by passing only the id? I see that you can use the API by sending an ID[] array, but I'm not sure how to use that through Apex. 

 

This should explain what I'm attempting to do... 

public void deleteRecords() {
	list<Id> ids;
	// add an account
	ids.add('001____________');
	// add a contact
	ids.add('003____________');
	// add a custom object
	ids.add('a0Y____________');
	// delete them all
	delete(ids);
}
Best Answer chosen by Admin (Salesforce Developers) 
Sean TanSean Tan

You're on the right track... you can do something like this:

 

public void deleteRecords() {
	list<Id> ids = new list<Id>{};
	// add an account
	ids.add('001____________');
	// add a contact
	ids.add('003____________');
	// add a custom object
	ids.add('a0Y____________');
	// delete them all
	Database.delete(ids);
}

Now keep in mind, though this will work, all your triggers for the obejcts in the list being deleted will fire (the delete triggers that is), so if theres any conflicts with them (e.g. you're expecting the triggers to fire in a certain order) then its probably not a good idea to use this approach. However if there are no dependencies then it should be fine.

All Answers

Sean TanSean Tan

You're on the right track... you can do something like this:

 

public void deleteRecords() {
	list<Id> ids = new list<Id>{};
	// add an account
	ids.add('001____________');
	// add a contact
	ids.add('003____________');
	// add a custom object
	ids.add('a0Y____________');
	// delete them all
	Database.delete(ids);
}

Now keep in mind, though this will work, all your triggers for the obejcts in the list being deleted will fire (the delete triggers that is), so if theres any conflicts with them (e.g. you're expecting the triggers to fire in a certain order) then its probably not a good idea to use this approach. However if there are no dependencies then it should be fine.

This was selected as the best answer
Andrew84Andrew84
Very cool! I'm just looking into this for a few upcoming projects.

So the deletes are handled asynchronously? Good information to know. Thank you!
Sean TanSean Tan

I wouldn't really use the word async, they're not happening async in the Apex context, just the records are being deleted in "parallel" relative to the object types being deleted (I doubt it's really parallel parallel).

Andrew84Andrew84

I just tested this out. It appears to be deleting records syncronously based on the order of the list, although this could just be a coincidence. 

 

This does not work because the contacts related to accounts will be deleted once the accounts are deleted.

list<Id> ids = new list<Id>();
for(Account a : [select id from Account]){
	ids.add(a.Id);
}
for(Contact c : [select id from Contact]){
	ids.add(c.Id);
}
// delete them all
database.delete(ids);

 

This however does work.

list<Id> ids = new list<Id>();
for(Contact c : [select id from Contact]){
	ids.add(c.Id);
}
for(Account a : [select id from Account]){
	ids.add(a.Id);
}
// delete them all
database.delete(ids);

 

Thank you for your help!