Dropping a Document Collection with SODA for In-Database JavaScript
You use SodaCollection.drop()
to drop an existing
collection.
Caution:
Do not use SQL to drop the database table that underlies a collection. Dropping a collection involves more than just dropping its database table. In addition to the documents that are stored in its table, a collection has metadata, which is also persisted in Oracle Database. Dropping the table underlying a collection does not also drop the collection metadata.
Note:
Day-to-day use of a typical application that makes use of SODA does not require that you drop and re-create collections. But if you need to do that for any reason then this guideline applies.
Do not drop a collection and then re-create it with different metadata if there is any application running that uses the collection in any way. Shut down any such applications before re-creating the collection, so that all live SODA objects are released.
There is no problem just dropping a collection. Any read or write operation on a dropped collection raises an error. And there is no problem dropping a collection and then re-creating it with the same metadata. But if you re-create a collection with different metadata, and if there are any live applications using SODA objects, then there is a risk that a stale collection is accessed, and no error is raised in this case.
In SODA implementations that allow collection metadata caching, such as SODA for Java, this risk is increased if such caching is enabled. In that case, a (shared or local) cache can return an entry for a stale collection object even if the collection has been dropped.
Note:
Commit all writes to a collection before using
SodaCollection.drop()
. For the method to succeed, all
uncommitted writes to the collection must first be committed. Otherwise, an
exception is raised.
Example 8-5 Dropping a Collection
This example shows how to drop a collection.
export function openAndDropCollection(collectionName) {
// look the collection up
const col = soda.openCollection(collectionName);
if (col === null) {
throw new Error (`No such collection ${collectionName}`);
}
// drop the collection - POTENTIALLY DANGEROUS
col.drop();
}
Parent topic: Using SODA for In-Database JavaScript