function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
swathi medak 9swathi medak 9 

delete multiple records using SOQL

Hi, I am learning SF developer. I created 100 records in Account object using SQL query. then im trying to delete all those records using SOQL query and for loop here is my code:
list<Account> newlist=new list<Account>();
newlist=[SELECT Id,Name FROM Account WHERE Name='test name'];
system.debug('list'+newlist);
list<Account> deletelist=new list<Account>();
for(Account a: newlist){
    a.Name='test name';
    deletelist.add(a);
}
system.debug('list'+deletelist);
delete deletelist;
but i am unable to delete. i know i can use work bench for this but i want to delete using SOQL here is the error im facing:
Line: 10, Column: 1
System.DmlException: Delete failed. First exception on row 0 with id 0016F00003O5AOEQA3; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, acountdeletetrigger: execution of BeforeDelete caused by: System.NullPointerException: Attempt to de-reference a null object Class.accountdeleteclass.deletecheck: line 3, column 1 Trigger.acountdeletetrigger: line 3, column 1: []
please tell me whats wrong thanks
Best Answer chosen by swathi medak 9
Shanu Kumar 18Shanu Kumar 18
Hi Swathi,
There is been a trigger you have written name as Trigger:- acountdeletetrigger. This trigger is causing the error. You can check your trigger or you can stop the trigger and then try to delete the record.
Thanks

All Answers

Deepali KulshresthaDeepali Kulshrestha
Hi Swathi,

I reviewed your code and the error in your code is you cannot perform DML operation (INSERT, UPDATE, DELETE, UPSERT, UNDELETE) inside a for-loop. So just query the list of account records and delete that list. Please try the following code:

List<Account> newList = new list<Account>();
newList = [SELECT Id,Name FROM Account WHERE Name='test name'];
system.debug('list'+newList);
delete newList;

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
swathi medak 9swathi medak 9
Hi please view my code again, i didnt mention the delete operation in for loop. i created a new list called delete list and added all those records with name test name and wrote the delete DML outside the for loop. Thanks
swathi medak 9swathi medak 9
Hi I resolved the issue, there was actually an account delete trigger that i created in the past, which is not allowing the SOQL query to perform the delete action. I deactivated the trigger and then tried to execute the code and it worked. Thanks .
Shanu Kumar 18Shanu Kumar 18
Hi Swathi,
There is been a trigger you have written name as Trigger:- acountdeletetrigger. This trigger is causing the error. You can check your trigger or you can stop the trigger and then try to delete the record.
Thanks
This was selected as the best answer
Vipul-GoyalVipul-Goyal
Hey,
I just built a Library to delete bulk records in apex.
You can delete them in user or system mode in a batch size that suits you.
There are a host of other features too.
To use the library you can go to : https://github.com/vipul-goyal/Salesforce-Batch-To-Delete-Records
The code is in DX format.

Please star the repository if you find it worth it.

Thanks  
shubham shrawankarshubham shrawankar
I have a problem 
i m written dml for delete operation 
But i m delete my single record but i m trying to delete a more records at tym how does do it..