I have a database in hive which has around 100 tables. I would like to delete the whole database in a single shot query.
How can we achieve that in Hive?
I have a database in hive which has around 100 tables. I would like to delete the whole database in a single shot query.
How can we achieve that in Hive?
Use,
DROP DATABASE database_name CASCADE;
By default, the mode is RESTRICT
which blocks the deletion of database if it holds tables.
You can use this command:
DROP DATABASE IF EXISTS HIVE_DATABASE_NAME CASCADE;
In case, you are using older version of Hive: Drop all tables and views from hive database first, and then drop the database. You can consolidate below command in a shell script to execute.
hive -e 'use HIVE_DATABASE_NAME;show tables' | xargs -I '{}' hive -e 'use HIVE_DATABASE_NAME;DROP TABLE IF EXISTS {}'
hive -e 'use HIVE_DATABASE_NAME;show tables' | xargs -I '{}' hive -e 'use HIVE_DATABASE_NAME;DROP VIEW IF EXISTS {}'
hive -e 'DROP DATABASE IF EXISTS HIVE_DATABASE_NAME;'
General syntax is as follows:
hive> DROP DATABASE [IF EXISTS] database_name [RESTRICT|CASCADE];
The default behaviour is RESTRICT, where DROP DATABASE will fail if the database is not empty. To drop the tables in the database as well, use DROP DATABASE … with CASCADE option.
1. Drop database without table or Empty Database:
hive> DROP DATABASE database_name;
2. Drop database with tables:
hive> DROP DATABASE database_name CASCADE;
It dropping respective tables before dropping the database.