8

I am working on an application in which I want to delete all SMS from inbox. For that I have used the following code.

Uri uriSms = Uri.parse("content://sms/inbox");
Cursor c = getContentResolver().query(uriSms, null,null,null,null); 
int id = c.getInt(0);
int thread_id = c.getInt(1); //get the thread_id
getContentResolver().delete(Uri.parse("content://sms/conversations/" + thread_id),null,null);

This code does not work. Is there any way to do the same?

Jaydeepsinh Jadeja
  • 391
  • 2
  • 10
  • 20

2 Answers2

7

The delete uri is "content://sms/" + id;

Uri inboxUri = Uri.parse("content://sms/inbox");
int count = 0;
Cursor c = context.getContentResolver().query(inboxUri , null, null, null, null);
while (c.moveToNext()) {
    try {
        // Delete the SMS
        String pid = c.getString(0); // Get id;
        String uri = "content://sms/" + pid;
        count = context.getContentResolver().delete(Uri.parse(uri),
                null, null);
    } catch (Exception e) {
    }
}
return count;
Mariusz Jamro
  • 30,615
  • 24
  • 120
  • 162
1
//delete all call logs
Uri callLog = Uri.parse("content://call_log/calls");
int rs1 = getContentResolver().delete(callLog, null, null);

//delete all sms
Uri inboxUri = Uri.parse("content://sms/");       
int rs2 = getContentResolver().delete(inboxUri, Sms._ID + "!=?", new String[]{"0"});
Tosun Can
  • 11
  • 2