Officially out now: The TypeDB 3.0 Roadmap

Delete query

In this guide, you’ll see how to delete data from a database using Studio, Console, or one of the drivers.

Understanding Delete queries

A Delete query is used to delete data from a TypeDB database. A delete clause is executed once per every result matched by the match clause of the same Delete query. For a detailed explanation of the query see the Delete query page.

Sending Delete queries

Before you begin, make sure you followed the instructions from the Insert query page. The easiest way to send a Delete query is to use Studio or Console:

  • Studio

  • Console

Follow the Studio manual to connect to TypeDB and select a project folder. Then use the steps below:

  1. Use a drop-down list in the top toolbar to select a database.

  2. Switch to data session and write transaction types.

  3. Open a new tab and insert or type in a Delete query, for example:

    TypeQL Delete query
    match
    $u1 isa user, has name "Bob";
    $u2 isa user, has name "Charlie";
    $f($u1,$u2) isa friendship;
    delete
    $f isa friendship;
  4. Run the query by clicking the studio run Run Query button.

  5. Commit the changes by clicking the studio check Commit query button.

  1. Open a data session and write transaction to the selected database (e.g., sample_db):

    transaction sample_db data write
  2. Send the Delete query:

    match
    $u1 isa user, has name "Bob";
    $u2 isa user, has name "Charlie";
    $f($u1,$u2) isa friendship;
    delete
    $f isa friendship;

    Push Enter twice to send the query.

  3. Commit the changes:

    commit

To send a Delete query programmatically, use drivers:

  • Rust

  • Python

  • Node.js

  • Java

  • C#

  • C++

  • C

Follow the connection guide to connect the driver to TypeDB Cloud or TypeDB Core.

Open a data session to the selected database, open a write transaction, and use the transaction.query().delete() method:
let db = databases.get(DB_NAME)?;
{
    let session = Session::new(db, SessionType::Data)?;
    {
        let tx = session.transaction(TransactionType::Write)?;
        let delete_query = "
                            match
                            $u isa user, has name 'Charlie';
                            $f ($u) isa friendship;
                            delete
                            $f isa friendship;
                            ";
        let _ = tx.query().delete(delete_query).resolve();
        tx.commit().resolve()?;
    }
}

Follow the connection guide to connect the driver to TypeDB Cloud or TypeDB Core.

Open a data session to the selected database, open a write transaction, and use the transaction.query.delete() method:
with driver.session(DB_NAME, SessionType.DATA) as session:
    with session.transaction(TransactionType.WRITE) as tx:
        delete_query = """
                        match
                        $u isa user, has name "Charlie";
                        $f ($u) isa friendship;
                        delete
                        $f isa friendship;
                        """
        response = tx.query.delete(delete_query).resolve()
        tx.commit()

Follow the connection guide to connect the driver to TypeDB Cloud or TypeDB Core.

Open a data session to the selected database, open a write transaction, and use the transaction.query.delete() method:
try {
    session = await driver.session(DB_NAME, SessionType.DATA);
    try {
        tx = await session.transaction(TransactionType.WRITE);
        const delete_query = `
                            match
                            $u isa user, has name "Charlie";
                            $f ($u) isa friendship;
                            delete
                            $f isa friendship;
                            `;
        await tx.query.delete(delete_query);
        await tx.commit();
    }
    finally {if (tx.isOpen()) {await tx.close()};}
}
finally {await session?.close();}

Follow the connection guide to connect the driver to TypeDB Cloud or TypeDB Core.

Open a data session to the selected database, open a write transaction, and use the transaction.query().delete() method:
try (TypeDBSession session = driver.session(DB_NAME, TypeDBSession.Type.DATA)) {
    try (TypeDBTransaction tx = session.transaction(TypeDBTransaction.Type.WRITE)) {
        String deleteQuery = """
                            match
                            $u isa user, has name "Charlie";
                            $f ($u) isa friendship;
                            delete
                            $f isa friendship;
                            """;
        tx.query().delete(deleteQuery).resolve();
        tx.commit();
    }
}

Follow the connection guide to connect the driver to TypeDB Cloud or TypeDB Core.

Open a data session to the selected database, open a write transaction, and use the transaction.Query.Delete() method:
using (ITypeDBSession session = driver.Session(DB_NAME, SessionType.Data)) {
    using (ITypeDBTransaction tx = session.Transaction(TransactionType.Write)) {
        string delete_query = @"
                        match
                        $u isa user, has name 'Charlie';
                        $f ($u) isa friendship;
                        delete
                        $f isa friendship;";
        tx.Query.Delete(delete_query).Resolve();
        tx.Commit();
    }
}

Follow the connection guide to connect the driver to TypeDB Cloud or TypeDB Core.

Open a data session to the selected database, open a write transaction, and use the transaction.query.delete() method:
TypeDB::Options options;
TypeDB::Session session = driver.session(DB_NAME, TypeDB::SessionType::DATA, options);
{
    TypeDB::Transaction tx = session.transaction(TypeDB::TransactionType::WRITE, options);
    std::string deleteQuery = R"(
                                match
                                $u isa user, has name "Charlie";
                                $f ($u) isa friendship;
                                delete
                                $f isa friendship;
                                )";
    tx.query.matchDelete(deleteQuery).get();
    tx.commit();
}

Follow the connection guide to connect the driver to TypeDB Cloud or TypeDB Core.

Open a data session to the selected database, open a write transaction, and use the query_delete() method:
Options* opts = options_new();
session = session_new(databaseManager, DB_NAME, Data, opts);
tx = transaction_new(session, Write, opts);
if ((tx == NULL) || FAILED()) {
    handle_error("Transaction failed to start.");
    goto cleanup;
}
char query[512];
snprintf(query, sizeof(query), "match $u isa user, has name 'Charlie'; $f ($u) isa friendship; delete $f isa friendship;");
void_promise_resolve(query_delete(tx, query, opts));
if (FAILED()) {
    handle_error("Query execution failed.");
    goto cleanup;
}
void_promise_resolve(transaction_commit(tx));
if (FAILED()) {
    handle_error("Transaction commit failed.");
    goto cleanup;
}
session_close(session);

Response interpretation

A Delete query returns a Promise of an empty response. This promise can be resolved when the query is finished executing, but it doesn’t contain any useful information.

Estimation with a read query

You can estimate what gets deletes in a Delete query by running a preliminary Fetch or Get query with the same match clause in the same transaction. Read queries can be used in a write transaction, and write transactions are snapshoted, preventing data changes committed in other transactions from influencing the results.

You can send any type of read query, but the most direct approach is to send an aggregated Get query to count the number of matches. The match clause should be exactly the same as in the Delete query you are trying to estimate.

Checking the number of matched results
match
$u1 isa user, has name "Bob";
$u2 isa user, has name "Charlie";
$f($u1,$u2) isa friendship;
get; count;

The response is a single value of a long value type. The aggregated number of matched results is equal to the number of times the delete clause of a Delete query with the same match clause in the same write transaction will be executed. Every execution of a delete clause tries to delete the data specified by a pattern. If the data is deleted already by previous execution of the delete clause in the same query, no additional data is deleted and no error is produced.

Learn more

More about a Delete query

Learn more about Delete queries in TypeQL: syntax, behavior, and query examples.

Learn how to update or replace data from a database with a single query.

Check out the Writing data section of TypeDB Academy.