REMOVE statementThe REMOVE statement is used to remove resources such as databases, tables, indexes, events and more. Similar to an SQL DROP statement.
SurrealQL SyntaxREMOVE [ NAMESPACE [ IF EXISTS ] @name | DATABASE [ IF EXISTS] @name | USER [ IF EXISTS ] @name ON [ ROOT | NAMESPACE | DATABASE ] | ACCESS [ IF EXISTS ] @name ON [ NAMESPACE | DATABASE ] | EVENT [ IF EXISTS ] @name ON [ TABLE ] @table | FIELD [ IF EXISTS ] @name ON [ TABLE ] @table | INDEX [ IF EXISTS ] @name ON [ TABLE ] @table | ANALYZER [ IF EXISTS ] @name | FUNCTION [ IF EXISTS ] fn::@name | PARAM [ IF EXISTS ] $@name | TABLE [ IF EXISTS ] @name ]
The following queries show an example of how to remove resources.
REMOVE NAMESPACE surrealdb; REMOVE DATABASE blog; REMOVE USER writer ON NAMESPACE; REMOVE USER writer ON DATABASE; REMOVE ACCESS token ON NAMESPACE; REMOVE ACCESS user ON DATABASE; REMOVE EVENT new_post ON TABLE article; -- Only works for Schemafull tables (i.e. tables with a schema) REMOVE FIELD tags ON TABLE article; REMOVE INDEX authors ON TABLE article; REMOVE ANALYZER example_ascii; REMOVE FUNCTION fn::update_author; REMOVE PARAM $author; REMOVE TABLE article;
Available since: v1.3.0
The following queries show an example of how to remove resources using the IF EXISTS clause, which will only remove the resource if it exists.
REMOVE NAMESPACE IF EXISTS surrealdb; REMOVE DATABASE IF EXISTS blog; REMOVE USER IF EXISTS writer ON NAMESPACE; REMOVE USER IF EXISTS writer ON DATABASE; REMOVE ACCESS IF EXISTS token ON NAMESPACE; REMOVE ACCESS IF EXISTS user ON DATABASE; REMOVE EVENT IF EXISTS new_post ON TABLE article; -- Only works for Schemaful tables (i.e. tables with a schema) REMOVE FIELD IF EXISTS tags ON TABLE article; REMOVE INDEX IF EXISTS authors ON TABLE article; REMOVE ANALYZER IF EXISTS example_ascii; REMOVE FUNCTION IF EXISTS fn::update_author; REMOVE PARAM IF EXISTS $author; REMOVE TABLE IF EXISTS article;
Available since: v3.0.0-alpha.14
A table used as a source for a table view cannot be removed until the table view itself has been removed.
DEFINE TABLE pc; DEFINE TABLE pc_agg AS SELECT count(), class FROM pc GROUP BY class; CREATE |pc:3| SET class = "Wizard"; CREATE |pc:10| SET class = "Warrior"; SELECT * FROM pc_agg; -- Error: pc_agg requires pc to work REMOVE TABLE pc; REMOVE TABLE pc_agg; -- pc_agg is now gone, pc can be removed too REMOVE TABLE pc;
The SELECT * FROM pc_agg query shows that the table view is pulling data from the pc table. As long as the pc table exists, pc_agg cannot be removed.
-------- Query -------- [ { class: 'Warrior', count: 10, id: pc_agg:['Warrior'] }, { class: 'Wizard', count: 3, id: pc_agg:['Wizard'] } ] -------- Query -------- 'Invalid query: Cannot delete table `pc` on which a view is defined, table(s) `pc_agg` are defined as a view on this table.'