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
SuvraSuvra 

SOQL delete syntax

Hi,

I want to delete records from a custom table. Can anybody tell me what is the exact commend for deleting set of records in SOQL?

 

Thanks,

Suvra

SuperfellSuperfell
there is no soql command to delete rows. if your delete criteria is based on a where clause, you can do a query, then loop over the results to perform the delete.
hisrinuhisrinu

The delete DML operation deletes one or more existing sObject records, such as individual accounts or contacts, from your organization’s data. delete is analogous to the DELETE statement in the Force.comWeb Services API.
DML Statement Syntax as follows.


delete sObject | Record.ID

 

delete sObject[] | LIST:ID[]

SuvraSuvra

Hi All,

 

Thanks a lot for your suggestions. I tried the same with the following code :

 

 

List<suvra__marker__c>  delmarker = new List<suvra__marker__c>(); 

delmarker=[select suvra__Street__c,suvra__City__c,suvra__State__c,suvra__Postal_Code__c,
            suvra__Country__c,suvra__Latitude__c,suvra__Longitude__c from suvra__marker__c
            where suvra__Street__c = null or
            suvra__City__c = null or suvra__State__c = null or suvra__Postal_Code__c = null or
            suvra__Record_Identifier__c = null or
            suvra__Latitude__c = null or suvra__Longitude__c = null];           
            
 delete delmarker;

 

But it is unable to delete the records from the custom table 'marker'. Kindly let me know if I'm missing anything here, or if there is any syntax error in the code.

 

Thanks

venkateshvenkatesh

List<suvra__marker__c>  delmarker = new List<suvra__marker__c>(); 

delmarker=[select suvra__Street__c,suvra__City__c,suvra__State__c,suvra__Postal_Code__c,
            suvra__Country__c,suvra__Latitude__c,suvra__Longitude__c from suvra__marker__c
            where suvra__Street__c = null or
            suvra__City__c = null or suvra__State__c = null or suvra__Postal_Code__c = null or
            suvra__Record_Identifier__c = null or
            suvra__Latitude__c = null or suvra__Longitude__c = null];    

       
            for(suvra__marker__c sm:delmarker)
                  delete sm;

 

 

can u try this..it might work :-?

SuperfellSuperfell
an error message, compile error? how exactly is it unable to delete the records ?
SuvraSuvra

Hi,

 

There is no error message or compilation error over here, but the records are not getting deleted from table.

 

Venkatesh, I tried your approach also, its giving the same problem.

SuperfellSuperfell
then its seems likely that the query results no rows. you'll need do some debugging.
venkateshvenkatesh
yes thats the only option , query is getting 0 rows..try one generic SOQL..which gives all the rows..cheers
ethanoneethanone

This worked for me:

 

 

List<Object__c> objs = new List<Object__c>();

objs = [Select o.Field From Object__c o];

for (Object__c obj : objs){

delete obj;

}

 

 

 

Message Edited by ethanone on 07-28-2009 10:00 AM
FilipeMFilipeM
List<Object__c> toDel = new List<Object__c>();
toDel = [select id from Object__c];

delete toDel;

 I wanted to delete all rows in Object__c. This worked for me.

 

Cheers,

Filipe

Volunteer

Amnesty International Portugal

 

JLumbJLumb

Was just looking up how to do this, it can be done even easier with just:

 

delete [select id from Object__c];

 

Rajesh AnnamRajesh Annam
delete [ select id from Object__c];
use it use proper condition in where clause, If you want to delete all records use above, but be sure what you are deleting
Collin YeadonCollin Yeadon
List<suvra__marker__c>  delmarker = new List<suvra__marker__c>(); 

Just want to add that it is wasteful to declare the list and assign it to a new list as the SOQL will create a list and assign it to "delmarker" even if there are not any results.

After so many years, I don't think this question should still be showing up as "unanswered question".  The answer was given and the if anyone stumbling upon this question has a similar problem, remember to add a debug message and check the size of the resulting object list..

ie.

List<Contact> contactResult = [SELECT Name FROM Contact WHERE LastName='CrazyLocoChicken'];

System.Debug('SOQL Returned ' + contactResult.size() + ' contacts');

if (contactResult.size() > 0){
   delete contactResult;
}
Steve Moore 4Steve Moore 4
This is what I use for batch deletes.
 
qr = binding.query(sql);

if (qr.size > 0)
{
	bool bContinue = true;

	while (bContinue)
	{
		List<string> list = new List<string>();

		for (int i = 0; i < qr.records.Length; i++)
		{
			Gifts_and_Loans_of_Art__c sf_gift_objs = (Gifts_and_Loans_of_Art__c)qr.records[i];
			list.Add(sf_gift_objs.Id);
		}

		string[] ids = list.ToArray();

		// Max you can delete is 200 items at a time
		for (int i = 0; i < ids.Length; i = i + 200)
		{
			string[] items = ids.Skip(i).Take(200).ToArray();
			DeleteResult[] dr = binding.delete(items);
		}

		Console.WriteLine(ids.Length + " items deleted.");
		Log.Information(ids.Length + " items deleted.");

		//handle the loop + 1 problem by checking the most recent queryResult 
		if (qr.done)
			bContinue = false;
		else
			qr = binding.queryMore(qr.queryLocator);
	}
}
}


A single row delete is much simpler.
 

string[] ids = { myId };
DeleteResult[] del = binding.delete(ids);