Programmatically delete SMS in android using python

Deleting SMS text has been on of issues with default SMS clients of Andriod OS. Issue 5669 talks a lot about it.

The most common alternative is to use different SMS clients, that allows bulk delete.

But using some simple python script, deleting SMS from inbox is just a click away, provided one has SL4A installed on the device.

Simple python script to delete SMS from inbox in Android devices :

import android

droid = android.Android()
print droid.smsGetMessages(False,'inbox', None)
msgs = droid.smsGetMessageIds(False, 'inbox')[1]

for msg in msgs:
    print msg
    droid.smsDeleteMessage(msg)

The above code removes all the messages that where received, to remove sent also along with it, it's easier again python exhibits it's power ;)

Just extend the msgs list to have sent msgs also by doing :

import android

droid = android.Android()
msgs = droid.smsGetMessageIds(False, 'inbox')[1]
msgs.extend(droid.smsGetMessageIds(False,'sent')[1])

for msg in msgs:
    droid.smsDeleteMessage(msg)

Note the API smsGetDeleteMessageIds() signature is as below :

smsGetMessageIds(
Boolean unreadOnly,
String folder[optional, default inbox])

Returns a List of all message IDs.

So the first parameter lets one to decided if to delete read or unread messages.

Yes a simple menu to prompt users to select if the code must delete read on unread is easy enough using the dialogSetMultiChoiceItems()
But this was just to demonstrate the easy of python coding in android.

Share this