24. Deleting SMS Messages from Message Storage (AT+CMGD)
The AT command +CMGD (command name in text: Delete Message) is used to delete SMS message(s) from message storage. The message storage area from which SMS messages are deleted is specified by the +CPMS AT command (command name in text: Preferred Message Storage). Details about the +CPMS AT command can be found in the earlier section "Preferred Message Storage (AT+CPMS)" of this SMS tutorial.
24.1. Syntax of the +CMGD AT Command
The syntax of the +CMGD AT command is: (Optional parameters are enclosed in square brackets.)
+CMGD=index[,flag]
In the above line, index is an integer specifying the location of the SMS message to be deleted from the message storage area by the +CMGD AT command, and flag is an integer specifying whether to delete SMS messages according to their message status. The SMS specification has defined these flag values: 0, 1, 2, 3 and 4.
0. Meaning: Delete only the SMS message stored at the location index from the message storage area. This is the default value.
1. Meaning: Ignore the value of index and delete all SMS messages whose status is "received read" from the message storage area.
2. Meaning: Ignore the value of index and delete all SMS messages whose status is "received read" or "stored sent" from the message storage area.
3. Meaning: Ignore the value of index and delete all SMS messages whose status is "received read", "stored unsent" or "stored sent" from the message storage area.
4. Meaning: Ignore the value of index and delete all SMS messages from the message storage area.
To find the flag values supported by your GSM/GPRS modem or mobile phone, you can perform a test operation using the +CMGD AT command. See the next section for the details.
24.2. Finding the Supported Indexes and Flag Values
Performing a test operation with the +CMGD AT command allows you to find all indexes that are supported by the index parameter. The test operation may also return all supported values of the flag parameter. Here is the format of the information response returned after the execution of the test command "+CMGD=?": (Optional fields are enclosed in square brackets.)
+CMGD: (list_of_indexes)[,(list_of_flag_values)]
The values inside the first parentheses are the values that are supported by the index parameter of the +CMGD AT command.
The values inside the second parentheses are the values that are supported by the flag parameter of the +CMGD AT command. Note: A GSM/GPRS modem or mobile phone might not provide these values since the SMS specification defines them as optional.
As an example, here is the response returned from my Nokia 6021 mobile phone to Windows' HyperTerminal program after the execution of the test command "+CMGD=?":
AT+CMGD=?
+CMGD:
(1-10),(0-4)
OK
From above, we know that the Nokia 6021 mobile phone allows any integer between 1 and 10 to be assigned to the index parameter of the +CMGD AT command. Also, the Nokia 6021 mobile phone supports all of the five flag values defined in the SMS specification.
Do you remember that we described the format of values inside parentheses in the section "Test Command -- Checks Whether a Certain AT Command is Supported"? If not, you may want to go back and have a look. Here are a few examples showing some of the possibilities: "(0-4)", "(0,1,2,3,4)", "(0,2-4)".
24.3. Example Demonstrating How to Use the +CMGD AT Command to Delete SMS Text Messages
Now let's see an example that demonstrates how to use the +CMGD AT command to delete an SMS text message from the message storage area. Suppose you have written an SMS text message to the message storage area by the +CMGW AT command (details about the +CMGW AT command can be found in the earlier section "Writing SMS Messages to Memory / Message Storage (AT+CMGW)" of this SMS tutorial), like this:
AT+CMGW="+85291234567"
>
A simple demo of SMS text messaging.
+CMGW: 1
OK
The information response of the +CMGW AT command tells us that the SMS text message is stored in the memory location at index 1. To delete the SMS text message, specify 1 to the index parameter of the +CMGD AT command, like this:
(Important note: Make sure you are writing text messages to and deleting text messages from the same message storage area. If not, you may delete the wrong text message. Details about message storage areas can be found in the earlier section "Preferred Message Storage (AT+CPMS)" of this SMS tutorial.)
AT+CMGD=1
OK
The final result code OK indicates the SMS text message "A simple demo of SMS text messaging." was deleted successfully. If the operation fails, the final result code returned will either be ERROR or +CMS ERROR.
Here is another example. Suppose you want to delete all SMS messages stored in the message storage area. To do this, assign 4 to the flags parameter of the +CMGD AT command. The value assigned to the index parameter does not matter since it will be ignored by the GSM/GPRS modem or mobile phone.
AT+CMGD=1,4
OK
Previous Page | Page 43 of 65 | Next Page |
- 1. Introduction to SMS Messaging
- 2. What Makes SMS Messaging So Successful Worldwide?
- 3. Example Applications of SMS Messaging
- 4. What is an SMS Center / SMSC?
- 5. Basic Concepts of SMS Technology
- 6. Intra-operator SMS Messages
- 7. Inter-operator SMS Messages
- 8. International SMS Messages
- 9. What is an SMS Gateway?
- 10. How to Send SMS Messages from a Computer / PC?
- 11. How to Receive SMS Messages Using a Computer / PC?
- 12. Introduction to GSM / GPRS Wireless Modems
- 13. How to Use Microsoft HyperTerminal to Send AT Commands to a Mobile Phone or GSM/GPRS Modem?
- 14. Introduction to AT Commands
- 15. General Syntax of Extended AT Commands
- 16. Result Codes of AT Commands
- 17. AT Command Operations: Test, Set, Read and Execution
- 18. Testing the Communication between the PC and GSM/GPRS Modem or Mobile Phone
- 19. Checking if the GSM/GPRS Modem or Mobile Phone Supports the Use of AT Commands to Send, Receive and Read SMS Messages
- 20. Operating Mode: SMS Text Mode and SMS PDU Mode
- 21. Setting or Reading the Service Center Address / SMSC Address (AT+CSCA)
- 22. Preferred Message Storage (AT+CPMS)
- 23. Writing SMS Messages to Memory / Message Storage (AT+CMGW)
- 24. Deleting SMS Messages from Message Storage (AT+CMGD)
- 25. Sending SMS Messages from a Computer / PC Using AT Commands (AT+CMGS, AT+CMSS)
- 26. Reading SMS Messages from a Message Storage Area Using AT Commands (AT+CMGR, AT+CMGL)
- 27. Appendix A: How to Choose an SMS Service Provider (SMS Gateway Provider, SMS Reseller, SMS Broker)?
- 28. Appendix B: Comparison Table of SMS Service Providers (SMS Gateway Providers, SMS Resellers, SMS Brokers)
- 29. Appendix C: Free Software/Tools and Libraries for Sending and Receiving SMS Messages with a Computer / PC
- 30. Appendix D: GSM 7-bit Default Alphabet Table (with Character Codes of ISO 8859 Latin 1)