Skip to content

Core\Queue::delete

Deletes the database row of one background job.

You pass the id that Core\Queue::push returned. delete removes the row of the job and returns true. It works for a waiting job, a finished job, a cancelled job and a job in the dead-letter table (the table of jobs that failed too often). It returns false when a worker is running the job, or when the row is already gone. After a delete, status throws an error for that job.

cancel only stops a job and keeps its row. Use delete when the data in the row must be gone, for example when a user deletes their account.

Good to know: the program needs the queue.purge capability for the queue of the job. A server has no such capability until the person who runs it adds one.

The examples below delete a waiting job, delete a cancelled job, and remove the jobs of a user who deleted their account.

Parameters

  • $jobCore\Queue\IdThe Core\Queue\Id that push returned. It still works after the job moves to the dead-letter table.

Return value

Returns bool. true if this call deleted the row. false if there was nothing to delete: a worker is running the job, the job is not in this queue, or an earlier delete deleted it. After true, the row is gone, so status throws an error for this job.

Errors

  • RuntimeErrorThe configuration grants no queue.purge for the queue of the job. 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 a waiting job

<?nvs
// Shows how to delete a job, and what `status` does after that.
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 $job = Queue::push('jobs/export.nvs', { queue: 'exports', arguments: ['report' => 'sales'] });
// The job is waiting, so `delete` removes its row. This prints `deleted: yes`.
echo 'deleted: ', Queue::delete($job) ? 'yes' : 'no', "\n";
// The row is gone, so `status` throws a `RuntimeError`.
// This prints `status: the job does not exist`.
try {
Queue::status($job);
echo "status: found\n";
} catch (RuntimeError $e) {
echo "status: the job does not exist\n";
}
Output
deleted: yes
status: the job does not exist

Delete a cancelled job

<?nvs
// Shows the difference between `cancel` and `delete`.
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, []);
class Rows
{
// Counts the rows in the jobs table.
public static function count(Core\Db\Connection $db): uint
{
return $db->query('select id from nvs_jobs', [])->count();
}
}
var $job = Queue::push('jobs/send-invoice.nvs', { queue: 'mail', arguments: ['order' => 1042] });
// `cancel` stops the job, but the row stays. This prints `after cancel: 1 row`.
Queue::cancel($job);
echo 'after cancel: ', Rows::count($db), " row\n";
// `delete` removes the row. This prints `after delete: 0 rows`.
Queue::delete($job);
echo 'after delete: ', Rows::count($db), " rows\n";
Output
after cancel: 1 row
after delete: 0 rows

Remove the jobs of a deleted account

<?nvs
// Shows how to delete the jobs of a user who deleted their account, so their data is gone.
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, []);
// The program kept the id of every job it added for this user.
// Each job has the email address of the user in its arguments.
array<Id> $userJobs = [
Queue::push('jobs/send-invoice.nvs', { queue: 'mail', arguments: ['to' => 'ana@example.com'] }),
Queue::push('jobs/send-newsletter.nvs', { queue: 'mail', arguments: ['to' => 'ana@example.com'] }),
Queue::push('jobs/export.nvs', { queue: 'exports', arguments: ['email' => 'ana@example.com'] }),
];
// The user deletes their account. The program deletes every job of the user.
// This prints `3 jobs deleted`.
int $deleted = 0;
foreach ($userJobs as Id $job) {
if (Queue::delete($job)) {
$deleted = $deleted + 1;
}
}
echo $deleted, " jobs deleted\n";
Output
3 jobs deleted