How to truncate a partitioned external table in hive?
Asked Answered
C

4

9

I'm planning to truncate the hive external table which has one partition. So, I have used the following command to truncate the table :

 hive> truncate table abc; 

But, it is throwing me an error stating : Cannot truncate non-managed table abc.

Can anyone please suggest me out regarding the same ...

Cassiani answered 12/11, 2018 at 6:52 Comment(0)
P
11

Make your table MANAGED first:

ALTER TABLE abc SET TBLPROPERTIES('EXTERNAL'='FALSE');

Then truncate:

truncate table abc;

And finally you can make it external again:

ALTER TABLE abc SET TBLPROPERTIES('EXTERNAL'='TRUE');
Palmation answered 12/11, 2018 at 7:37 Comment(3)
@leftjoin- when we set 'EXTERNAL'='FALSE' for an external table, will it move the file location date to hive warehouse or it just help us to truncate the table.Botswana
@vikrantrana truncate works only for managed tables, not external. Look at the docs: cwiki.apache.org/confluence/display/Hive/… It says "Currently the target table should be native/managed table or an exception will be thrown. "Palmation
I think is much better Rahul's solution. Change the purge property to the external table. It works and it is clean. ALTER TABLE mytable SET TBLPROPERTIES ('external.table.purge'='true'Bespectacled
C
3

By default, TRUNCATE TABLE is supported only on managed tables. Attempting to truncate an external table results in the following error:

Error: org.apache.spark.sql.AnalysisException: Operation not allowed: TRUNCATE TABLE on external tables

Action Required

Change applications. Do not attempt to run TRUNCATE TABLE on an external table.

Alternatively, change applications to alter a table property to set external.table.purge to true to allow truncation of an external table:

ALTER TABLE mytable SET TBLPROPERTIES ('external.table.purge'='true');

Conveyor answered 4/1, 2022 at 19:29 Comment(0)
R
2

There is an even better solution to this, which is basically a one liner.

insert overwrite table table_xyz select * from table_xyz where 1=2;

This code will delete all the files and create a blank file in the external folder location with absolute zero records.

Roncesvalles answered 21/7, 2021 at 15:52 Comment(2)
Have you tried that with partitioned table?Palmation
Partitioned table will remain as is. This is misleading answer.Palmation
H
0

Look at https://issues.apache.org/jira/browse/HIVE-4367 : use

truncate table my_ext_table force;
Hynes answered 8/12, 2022 at 13:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.