Drop a table
This Astra DB Serverless feature is currently in public preview. Development is ongoing, and the features and functionality are subject to change. Astra DB Serverless, and the use of such, is subject to the DataStax Preview Terms. The Data API tables commands are available through HTTP and the clients. If you use a client, tables commands are available only in client versions 2.0-preview or later. For more information, see Data API client upgrade guide. |
Deletes a table from a database, including all data stored in the table.
Method signature
-
Python
-
TypeScript
-
Java
-
curl
database.drop_table(
name: str,
*,
keyspace: str,
if_exists: bool,
table_admin_timeout_ms: int,
request_timeout_ms: int,
timeout_ms: int,
) -> None
database.dropTable(
name: string,
options?: {
ifExists?: boolean,
keyspace?: string,
timeout?: number,
}
): Promise<void>
void dropTable(String tableName)
void dropTable(
String tableName,
DropTableOptions dropTableOptions
)
curl -sS -L -X POST "ASTRA_DB_API_ENDPOINT/api/json/v1/ASTRA_DB_KEYSPACE" \
--header "Token: ASTRA_DB_APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"dropTable": {
"name": "TABLE_NAME"
}
}'
Result
-
Python
-
TypeScript
-
Java
-
curl
Deletes the specified table from the database, including all data stored in the table.
Does not return anything.
Deletes the specified table from the database, including all data stored in the table.
Returns a promise that resolves once the operation completes.
Deletes the specified table from the database, including all data stored in the table.
Does not return anything.
Deletes the specified table from the database, including all data stored in the table.
If the command succeeds, the response indicates the success.
Example response:
{
"status": {
"ok": 1
}
}
Parameters
-
Python
-
TypeScript
-
Java
-
curl
Name | Type | Summary |
---|---|---|
|
|
Either the name of a table or a |
|
|
If If |
|
|
A timeout, in milliseconds, for the underlying schema-changing HTTP request.
If not provided, the |
Name | Type | Summary |
---|---|---|
|
|
The name of the table to drop, if dropping through a |
|
|
The options for this operation |
Options (DropTableOptions
):
Name | Type | Summary |
---|---|---|
|
|
If If |
|
|
If dropping through a |
|
|
The client-side timeout for this operation. |
Name | Type | Summary |
---|---|---|
|
|
The name of the table to drop, if dropping through a |
|
Specialization of the operation:
|
Name | Type | Summary |
---|---|---|
|
|
The Data API command to delete a table in a Serverless (Vector) database. It acts as a container for all the attributes and settings required to delete the table. |
|
|
The name of the table to delete. |
Examples
The following examples demonstrate how to drop a table.
-
Python
-
TypeScript
-
Java
-
curl
Drop a table by name:
database.drop_table("fighters")
Drop a table through a Table
object:
my_table.drop()
Drop a table passing a Table
to a Database
:
database.drop_table(my_table)
Example:
database.list_table_names()
# ['fighters', 'games']
database.drop_table("fighters")
database.list_table_names()
# ['games']
# not erroring because of if_exists:
database.drop_table("fighters", if_exists=True)
Drop a table by name in the database’s working keyspace:
await db.dropTable('TABLE_NAME');
Drop a table by name in a specific keyspace:
await db.dropTable('TABLE_NAME', { keyspace: 'KEYSPACE' });
Drop a table through a Table
object:
await table.drop();
Example:
import { CreateTableDefinition, DataAPIClient, SomeRow } from '@datastax/astra-db-ts';
// Instantiate the client and connect to the database
const client = new DataAPIClient();
const db = client.db(process.env.CLIENT_DB_URL!, { token: process.env.CLIENT_DB_TOKEN! });
// Create table schema using bespoke Data API table definition syntax.
// For information about table definition and data types, see the documentation for createTable.
const TableDefinition = <const>{
columns: {
matchId: 'text'
round: 'tinyint',
mVector: { type: 'vector', dimension: 3 },
score: 'int',
when: 'timestamp',
winner: 'text',
fighters: { type: 'set', valueType: 'uuid' },
},
primaryKey: {
partitionBy: ['matchId'],
partitionSort: { round: 1 },
},
} satisfies CreateTableDefinition;
(async function () {
// Create an untyped table or error if a 'games' table already exists.
const table = await db.createTable<SomeRow>('games', { definition: TableDefinition });
// Drop a table from a database's working keyspace without checking if the table exists.
// If there is no match, the command succeeds but does nothing.
// If there is a match, the named table is deleted.
await db.dropTable(table.name);
// Use a Table object to run dropTable without checking if the table exists.
await table.drop();
// Use a Table object to drop a table only if the table exists.
// Errors if there is no match.
await table.drop({ ifExists: true });
})();
Drop a table by name in the database’s working keyspace:
database.dropTable("fighters");
Drop a table through a Table
object:
myTable.drop();
Drop a table through a database
instance with options:
DropTableOptions options = new DropTableOptions()
.ifExists(false)
.timeout(ofSeconds(5));
database.dropTable("games", options);
Example:
package com.datastax.astra.client.database;
import com.datastax.astra.client.DataAPIClients;
import com.datastax.astra.client.core.auth.EmbeddingAPIKeyHeaderProvider;
import com.datastax.astra.client.databases.Database;
import com.datastax.astra.client.tables.Game;
import com.datastax.astra.client.tables.Table;
import com.datastax.astra.client.tables.TableOptions;
import com.datastax.astra.client.tables.commands.options.CreateTableOptions;
import com.datastax.astra.client.tables.commands.options.DropTableOptions;
import com.datastax.astra.client.tables.definition.TableDefinition;
import com.datastax.astra.client.tables.definition.columns.ColumnDefinitionVector;
import com.datastax.astra.client.tables.definition.columns.ColumnTypes;
import com.datastax.astra.client.tables.definition.rows.Row;
import static com.datastax.astra.client.core.query.Sort.ascending;
import static com.datastax.astra.client.core.vector.SimilarityMetric.COSINE;
import static java.time.Duration.ofSeconds;
public class DropTable {
public static void main(String[] args) {
// Database astraDb = new DataAPIClient(token).getDatabase(endpoint);
Database db = DataAPIClients.localDbWithDefaultKeyspace();
// Drop without options
db.dropTable("games");
// Adding a timestamp
DropTableOptions options = new DropTableOptions()
.ifExists(false)
.timeout(ofSeconds(5));
db.dropTable("games", options);
}
}
curl -sS -L -X POST "ASTRA_DB_API_ENDPOINT/api/json/v1/ASTRA_DB_KEYSPACE" \
--header "Token: ASTRA_DB_APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"dropTable": {
"name": "TABLE_NAME"
}
}'
Example:
curl -sS -L -X POST "ASTRA_DB_API_ENDPOINT/api/json/v1/default_keyspace" \
--header "Token: ASTRA_DB_APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"dropTable": {
"name": "students"
}
}'
Client reference
-
Python
-
TypeScript
-
Java
-
curl
For more information, see the client reference.
For more information, see the client reference.
For more information, see the client reference.
Client reference documentation is not applicable for HTTP.