Drop database without deletion

Use Hive commands to drop a database without deleting the underlying storage folder.

Written by arvind.ravish

Last published at: May 24th, 2022

By default, the DROP DATABASE (AWS | Azure | GCP) command drops the database and deletes the directory associated with the database from the file system.

Sometimes you may want to drop the database, but keep the underlying database directory intact.

Example code

You can use this example code to drop the database without dropping the underlying storage folder.


import scala.collection.JavaConverters._
import org.apache.hadoop.hive.ql.metadata.Hive
import org.apache.hadoop.hive.conf.HiveConf
import org.apache.hadoop.hive.ql.session.SessionState

val hiveConf = new HiveConf(classOf[SessionState])
sc.hadoopConfiguration.iterator().asScala.foreach { kv =>
hiveConf.set(kv.getKey, kv.getValue)
sc.getConf.getAll.foreach {
case (k, v) => hiveConf.set(k, v)

hiveConf.setBoolean("hive.cbo.enable", false)
val state = new SessionState(hiveConf)
val hive = Hive.get(state.getConf)

hive.dropDatabase("<database-name>", false, false, true)

For more information on org.apache.hadoop.hive.ql.metadata.Hive, please review the Hive documentation.

Was this article helpful?