Core\Imap\Folder::delete
Deletes one message from a folder at once. The message cannot be brought back.
Pass the UID of the message (the number the server gives each message in a folder). Only that message is deleted. Other mail programs can mark messages for deletion too. delete does not remove those messages. If no message has that UID, delete throws an IOError.
To keep a copy, move the message to a folder such as Trash with move instead.
Good to know: the server must support deleting one message by its UID. Every common mail server does. If it does not, delete throws an IOError before it changes anything.
The examples below first add the message they delete. They use a directory block (a folder on disk that works like a mailbox, for tests and local work). In such a block, a message is a file, so Core\IO::write adds one. Each example then prints the same every time it runs.
Parameters
$uidintA UID (the number the server gives each message in a folder) thatsearchreturned.
Return value
Returns void. Nothing.
Errors
RuntimeError$uidis less than 1 or larger than 4294967295.IOErrorNo message has that UID, the server does not support deleting a single message, or the server does not answer.
Changelog
| Version | Change |
|---|---|
0.0.6 | Core\Imap reads a mailbox over IMAP (the protocol a program uses to read a mailbox on a server). A program opens an [imap.<name>] block of nvs.toml by its name, and then searches, fetches, moves, flags and deletes messages. For tests, a directory block reads a folder of .eml files instead of a server. |
Examples
The examples below run with this configuration file.
# Each example reads its own mailbox, a folder beside this file. They need no mail server.[imap.junk]directory = "junk"
[imap.shop]directory = "shop"
[imap.newsletter]directory = "newsletter"
# The program may open the three blocks.[capabilities.imap]connect = ["junk", "shop", "newsletter"]Delete a spam message
<?nvs// Shows how to delete one spam message from the `Junk` folder.
use Core\IO;use Core\Imap;
// A `directory` block is a folder on disk that works like a mailbox, for tests and local work.// In such a block, each message is a file. This line adds a spam message with the UID 2.IO::write('junk/Junk/2.eml', "From: Prize Office <win@example.net>\nSubject: You won a prize\n\nClick here.\n");
var $junk = Imap::open('junk')->folder('Junk');$junk->delete(2);
// Message 1 is not spam, so it stays. This prints "1 My new address".foreach ($junk->search() as int $uid) { echo $uid, ' ', $junk->fetch($uid)->subject(), "\n";}1 My new addressOnly the named message is deleted
<?nvs// Shows that `delete` deletes only the message you name, even when another message is marked for deletion.
use Core\IO;use Core\Imap;
// A `directory` block is a folder on disk that works like a mailbox, for tests and local work.// In such a block, each message is a file. This line adds a message with the UID 3.IO::write('shop/INBOX/3.eml', "From: Cy Okafor <cy@example.net>\nSubject: Test\n\nThis is a test.\n");
// `\Deleted` is the flag that marks a message for deletion.// Another mail program gave message 2 this flag. `delete(3)` does not remove message 2.var $inbox = Imap::open('shop')->folder('INBOX');$inbox->delete(3);
// This prints messages 1 and 2.foreach ($inbox->search() as int $uid) { echo $uid, ' ', $inbox->fetch($uid)->subject(), "\n";}
// Message 3 is deleted, so a second `delete` throws an `IOError`.try { $inbox->delete(3);} catch (IOError $gone) { echo $gone->message, "\n";}1 Where is my order?2 Can I change my address?Core\Imap\Folder::delete: the folder has no message with the UID 3Delete old newsletters
<?nvs// Shows a cleanup job that deletes the newsletters that are older than one week.
use Core\IO;use Core\Imap;use Core\Time\Date;use Core\Unit;
// A `directory` block is a folder on disk that works like a mailbox, for tests and local work.// In such a block, each message is a file. These lines add two old newsletters.IO::write('newsletter/INBOX/2.eml', "From: Shop News <news@example.com>\nSubject: Offers for October\nDate: Thu, 1 Oct 2026 07:00:00 +0000\n\nOffers.\n");IO::write('newsletter/INBOX/3.eml', "From: Shop News <news@example.com>\nSubject: Offers for September\nDate: Tue, 1 Sep 2026 07:00:00 +0000\n\nOffers.\n");
var $inbox = Imap::open('newsletter')->folder('INBOX');
// The job runs on 10 November 2026, so it deletes the newsletters from before 3 November.var $weekAgo = Date::at(2026, 11, 10)->minus(1, Unit::Week);foreach ($inbox->search({ from: 'news@example.com', before: $weekAgo }) as int $uid) { echo 'deleted: ', $inbox->fetch($uid)->subject(), "\n"; $inbox->delete($uid);}
// The reply from Ada is not a newsletter, so it stays.foreach ($inbox->search() as int $uid) { echo 'kept: ', $inbox->fetch($uid)->subject(), "\n";}deleted: Offers for Octoberdeleted: Offers for Septemberkept: Re: Our offers for November