Updated on 2024-04-29 GMT+08:00

Deleting a Table

Function Description

In HBase a table is deleted using the deleteTable method of org.apache.hadoop.hbase.client.Admin.

Sample Code

public void dropTable() {
  LOG.info("Entering dropTable.");
  Admin admin = null;
  try {
    admin = conn.getAdmin();
    if (admin.tableExists(tableName)) {
      // Disable the table before deleting it.
      admin.disableTable(tableName);
      // Delete table.
      admin.deleteTable(tableName);//Note [1]
    }
    LOG.info("Drop table successfully.");
  } catch (IOException e) {
    LOG.error("Drop table failed " ,e);
  } finally {
    if (admin != null) {
      try {
        // Close the Admin object.
        admin.close();
      } catch (IOException e) {
        LOG.error("Close admin failed " ,e);
      }
    }
  }
  LOG.info("Exiting dropTable.");
}

Precautions

Note [1] Only after the disableTable API is called, the table can be deleted by calling the deleteTable API. Therefore, deleteTable is often used together with disableTable, enableTable, tableExists, isTableEnabled, and isTableDisabled.