Skip to content

Core\Queue::purge

Deletes many jobs of one queue at once, and returns how many it deleted.

Without options, Core\Queue::purge deletes the jobs that succeeded or were cancelled. The state option deletes other jobs: State::Dead deletes the jobs in the dead-letter table (the table of jobs that failed too often), and State::Pending deletes jobs that have not run yet. Jobs that a worker is running are never deleted. The tag option deletes only the jobs with that tag. The before option deletes only the jobs added before that time.

One call deletes at most limit rows, and the default is 1000. A short delete keeps the table free for new jobs. To delete more, call purge again until it returns 0.

Good to know: the program needs the queue.purge capability for the queue.

The examples below delete cancelled jobs, delete the waiting jobs of one tag, and clean up a queue in batches.

Parameters

  • $queuestringThe name of the queue. It must match the name push used exactly. The queue.purge capability is checked for this name.

Options

  • stateCore\Queue::StateoptionalOne Core\Queue::State to delete in place of the default ones. Dead deletes from the dead-letter table. Pending deletes jobs that have not run yet. Claimed throws an error, because a worker is running those jobs.
  • tagstringoptionalDeletes only the jobs that push gave this tag.
  • beforeCore\Time\InstantoptionalDeletes only the jobs added before this time. This is the time of the push, not the time of the next attempt.
  • limituintoptionalThe most rows one call deletes, oldest first. The default is 1000. A delete without a limit can lock the table for a long time, and every push waits for it.

Return value

Returns uint. The number of rows this call deleted, and 0 when nothing matched. while (Core\Queue::purge('email') > 0) {} deletes all of them, one batch at a time.

Errors

  • LogicErrorstate is Core\Queue::State::Claimed. A worker is running those jobs, so their rows cannot be deleted.
  • RuntimeErrorThe configuration grants no queue.purge for this queue. A server has no such grant until the person who runs it adds one. It is also thrown when there is no [queue] block, or the database driver cannot run statements yet.
  • IOErrorThe connection to the database failed, or the database rejected the delete. The most common cause is that nvs queue migrate has not created the tables yet.

Examples

The examples below run with this configuration file.

nvs.toml
# The `jobs` database is kept in memory. It is deleted when the program ends.
[db.jobs]
driver = "sqlite"
path = ":memory:"
# The program may open the `jobs` database itself, to create the tables.
[capabilities.db]
connect = ["jobs"]
# The program may delete the jobs of the `mail` and `exports` queues.
# Without this list, `Core\Queue::delete` throws an error.
[capabilities.queue]
purge = ["mail", "exports"]
# `Core\Queue` saves its jobs in the `jobs` database.
# `workers = 0` starts no workers, so no job is started.
[queue]
connection = "jobs"
workers = 0

Delete cancelled jobs

<?nvs
// Shows how to delete the cancelled jobs of a queue.
use Core\Queue;
// On a real server, `nvs queue migrate` creates the queue tables.
// This database is in memory, so the example creates them itself.
var $db = Core\Db::connect('jobs');
var $jobs = <<<'SQL'
create table nvs_jobs (id integer primary key autoincrement, queue varchar(255) not null,
script text not null, args text, state smallint not null, attempts integer not null,
max_attempts integer not null, backoff_ms bigint not null, run_at bigint not null,
tag varchar(255), dedupe_key varchar(255), dedupe_pending varchar(255),
created_at bigint not null, claimed_at bigint, errors text, grants text, limits text,
non_transactional smallint, token text, constraint nvs_jobs_dedupe unique (dedupe_pending))
SQL;
var $dead = <<<'SQL'
create table nvs_dead_jobs (id bigint not null, queue varchar(255) not null,
script text not null, args text, attempts integer not null, max_attempts integer not null,
backoff_ms bigint not null, run_at bigint not null, tag varchar(255),
dedupe_key varchar(255), created_at bigint not null, failed_at bigint not null,
errors text not null, token text, primary key (id))
SQL;
$db->execute($jobs, []);
$db->execute($dead, []);
var $first = Queue::push('jobs/send-mail.nvs', { queue: 'mail', arguments: ['to' => 'ana@example.com'] });
var $second = Queue::push('jobs/send-mail.nvs', { queue: 'mail', arguments: ['to' => 'ben@example.com'] });
Queue::push('jobs/send-mail.nvs', { queue: 'mail', arguments: ['to' => 'eva@example.com'] });
Queue::cancel($first);
Queue::cancel($second);
// `purge` deletes the two cancelled jobs. The waiting job stays.
// This prints `deleted: 2` and `waiting: 1`.
echo 'deleted: ', Queue::purge('mail'), "\n";
echo 'waiting: ', Queue::stats('mail')->pending(), "\n";
Output
deleted: 2
waiting: 1

Delete the waiting jobs of one tag

<?nvs
// Shows how to stop a newsletter by deleting the jobs that have its tag.
use Core\Queue;
// On a real server, `nvs queue migrate` creates the queue tables.
// This database is in memory, so the example creates them itself.
var $db = Core\Db::connect('jobs');
var $jobs = <<<'SQL'
create table nvs_jobs (id integer primary key autoincrement, queue varchar(255) not null,
script text not null, args text, state smallint not null, attempts integer not null,
max_attempts integer not null, backoff_ms bigint not null, run_at bigint not null,
tag varchar(255), dedupe_key varchar(255), dedupe_pending varchar(255),
created_at bigint not null, claimed_at bigint, errors text, grants text, limits text,
non_transactional smallint, token text, constraint nvs_jobs_dedupe unique (dedupe_pending))
SQL;
var $dead = <<<'SQL'
create table nvs_dead_jobs (id bigint not null, queue varchar(255) not null,
script text not null, args text, attempts integer not null, max_attempts integer not null,
backoff_ms bigint not null, run_at bigint not null, tag varchar(255),
dedupe_key varchar(255), created_at bigint not null, failed_at bigint not null,
errors text not null, token text, primary key (id))
SQL;
$db->execute($jobs, []);
$db->execute($dead, []);
array<string> $readers = ['ana@example.com', 'ben@example.com', 'eva@example.com'];
foreach ($readers as string $to) {
Queue::push('jobs/newsletter.nvs', { queue: 'mail', tag: 'newsletter-june', arguments: ['to' => $to] });
}
Queue::push('jobs/send-invoice.nvs', { queue: 'mail', tag: 'invoice', arguments: ['to' => 'ana@example.com'] });
// `Queue::State::Pending` deletes jobs that have not run yet, and `tag` keeps the invoice.
// This prints `deleted: 3` and `waiting: 1`.
echo 'deleted: ', Queue::purge('mail', { state: Queue::State::Pending, tag: 'newsletter-june' }), "\n";
echo 'waiting: ', Queue::stats('mail')->pending(), "\n";
Output
deleted: 3
waiting: 1

Clean up a queue in batches

<?nvs
// Shows a clean-up task that deletes finished jobs a few rows at a time.
use Core\Queue;
use Core\Queue\Id;
// On a real server, `nvs queue migrate` creates the queue tables.
// This database is in memory, so the example creates them itself.
var $db = Core\Db::connect('jobs');
var $jobs = <<<'SQL'
create table nvs_jobs (id integer primary key autoincrement, queue varchar(255) not null,
script text not null, args text, state smallint not null, attempts integer not null,
max_attempts integer not null, backoff_ms bigint not null, run_at bigint not null,
tag varchar(255), dedupe_key varchar(255), dedupe_pending varchar(255),
created_at bigint not null, claimed_at bigint, errors text, grants text, limits text,
non_transactional smallint, token text, constraint nvs_jobs_dedupe unique (dedupe_pending))
SQL;
var $dead = <<<'SQL'
create table nvs_dead_jobs (id bigint not null, queue varchar(255) not null,
script text not null, args text, attempts integer not null, max_attempts integer not null,
backoff_ms bigint not null, run_at bigint not null, tag varchar(255),
dedupe_key varchar(255), created_at bigint not null, failed_at bigint not null,
errors text not null, token text, primary key (id))
SQL;
$db->execute($jobs, []);
$db->execute($dead, []);
// Five export jobs are added and then cancelled.
int $n = 0;
while ($n < 5) {
Id $job = Queue::push('jobs/export.nvs', { queue: 'exports', arguments: ['report' => $n] });
Queue::cancel($job);
$n = $n + 1;
}
// Each call deletes at most 2 rows. The loop stops when a call deletes nothing.
// This prints `deleted 2`, `deleted 2`, `deleted 1` and then `done: 5 jobs`.
uint $total = 0;
uint $deleted = Queue::purge('exports', { limit: 2 });
while ($deleted > 0) {
echo 'deleted ', $deleted, "\n";
$total = $total + $deleted;
$deleted = Queue::purge('exports', { limit: 2 });
}
echo 'done: ', $total, " jobs\n";
Output
deleted 2
deleted 2
deleted 1
done: 5 jobs