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:
-
Use a drop-down list in the top toolbar to select a database.
-
Switch to
data
session andwrite
transaction types. -
Open a new tab and insert or type in a Delete query, for example:
TypeQL Delete querymatch $u1 isa user, has name "Bob"; $u2 isa user, has name "Charlie"; $f($u1,$u2) isa friendship; delete $f isa friendship;
-
Run the query by clicking the Run Query button.
-
Commit the changes by clicking the Commit query button.
-
Open a
data
session andwrite
transaction to the selected database (e.g.,sample_db
):transaction sample_db data write
-
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.
-
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.
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.
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.
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.
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.
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.
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.
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.
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.