Drop database without deletion

By default, the DROP DATABASE 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)
println(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.