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
Venkat Reddy 45Venkat Reddy 45 

Query record and delete record

Dear Experts,

           I want to "Query record and delete record ", could you please give me code for this?

Regards,
Venkat.
Best Answer chosen by James Loghry
pconpcon
If you're doing this in Apex it's simply
Object__c obj = [select Id from Object__c where Id = 'xxxxx'];
delete obj;

Of course you'd want to do more with this like verifying that Id is valid, and that it exists.  And if you're doing this in a trigger you'll want to make this bulk ready.

If you are doing it via REST or SOAP or something like the Dataloader it's different.

All Answers

pconpcon
If you're doing this in Apex it's simply
Object__c obj = [select Id from Object__c where Id = 'xxxxx'];
delete obj;

Of course you'd want to do more with this like verifying that Id is valid, and that it exists.  And if you're doing this in a trigger you'll want to make this bulk ready.

If you are doing it via REST or SOAP or something like the Dataloader it's different.
This was selected as the best answer
Shivdeep KumarShivdeep Kumar
List<Object> o = New List<Object>();
For(Object obj : [Select id from Object where id='xxxxxxx']){
o.add(obj);
}
delete o;
James LoghryJames Loghry
In addition to pcon's example, you could also delete a list of records based on a criteria rather than an Id:
 
delete [Select Id From Object__c where Field__c = 'xxx'];