{"record":{"id":"2b22100b4ac49b02","repo":"prestodb/presto","slug":"schema-not-empty","errorCode":"SCHEMA_NOT_EMPTY","errorMessage":"Schema not empty: ","messagePattern":"Schema not empty: ","errorType":"error_code","errorClass":"PrestoException","httpStatus":null,"severity":"error","filePath":"presto-hive/src/main/java/com/facebook/presto/hive/HiveMetadata.java","lineNumber":1030,"sourceCode":"        });\n\n        Database database = Database.builder()\n                .setDatabaseName(schemaName)\n                .setLocation(location)\n                .setOwnerType(USER)\n                .setOwnerName(session.getUser())\n                .build();\n\n        metastore.createDatabase(getMetastoreContext(session), database);\n    }\n\n    @Override\n    public void dropSchema(ConnectorSession session, String schemaName)\n    {\n        // basic sanity check to provide a better error message\n        if (!listTables(session, schemaName).isEmpty() ||\n                !listViews(session, schemaName).isEmpty()) {\n            throw new PrestoException(SCHEMA_NOT_EMPTY, \"Schema not empty: \" + schemaName);\n        }\n        metastore.dropDatabase(getMetastoreContext(session), schemaName);\n    }\n\n    @Override\n    public void renameSchema(ConnectorSession session, String source, String target)\n    {\n        metastore.renameDatabase(getMetastoreContext(session), source, target);\n    }\n\n    @Override\n    public void createTable(ConnectorSession session, ConnectorTableMetadata tableMetadata, boolean ignoreExisting)\n    {\n        PrestoTableType tableType = isExternalTable(tableMetadata.getProperties()) ? EXTERNAL_TABLE : MANAGED_TABLE;\n        Table table = prepareTable(session, tableMetadata, tableType);\n        PrincipalPrivileges principalPrivileges = buildInitialPrivilegeSet(table.getOwner());\n        HiveBasicStatistics basicStatistics = table.getPartitionColumns().isEmpty() ? createZeroStatistics() : createEmptyStatistics();\n","sourceCodeStart":1012,"sourceCodeEnd":1048,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-hive/src/main/java/com/facebook/presto/hive/HiveMetadata.java#L1012-L1048","documentation":"dropSchema performs a sanity check before dropping the database in the metastore: if listTables or listViews report any objects in the schema, it throws SCHEMA_NOT_EMPTY. This is a safety guard so users get a clearer message than the metastore's own refusal to drop a non-empty database.","triggerScenarios":"Calling DROP SCHEMA on a schema that still contains at least one table or view (including views created in other connectors that map into this schema, or tables visible via the metastore but filtered in the current UI).","commonSituations":"Forgetting to drop child tables/views first; orphaned metastore entries from a partially deleted dataset; views defined in another catalog pointing at the schema that listViews still counts.","solutions":["Drop all tables first: run SHOW TABLES and SHOW VIEWS in the schema and DROP each one","Use DROP SCHEMA ... CASCADE (where supported) to drop the schema and its contents together","Check for hidden/orphan tables in the metastore (hive metastore client or SHOW TABLES) and clean them","If objects were already physically deleted, purge orphan metastore entries"],"exampleFix":"// before\nDROP SCHEMA sales;\n// after\nDROP TABLE sales.orders;\nDROP VIEW sales.daily;\nDROP SCHEMA sales;","handlingStrategy":"try-catch","validationCode":"// check emptiness before DROP SCHEMA\nList<String> tables = metadata.listTables(session, schemaName);\nList<String> views = metadata.listViews(session, schemaName);\nif (!tables.isEmpty() || !views.isEmpty()) {\n    throw new IllegalStateException(\"Schema not empty: \" + tables.size() + \" tables, \" + views.size() + \" views\");\n}","typeGuard":null,"tryCatchPattern":"try {\n    connector.dropSchema(session, schemaName);\n} catch (PrestoException e) {\n    if (e.getErrorCode().getCode() == StandardErrorCode.SCHEMA_NOT_EMPTY.code()) {\n        // enumerate and drop tables/views, then retry once\n    } else { throw e; }\n}","preventionTips":["Run SHOW TABLES and SHOW VIEWS before dropping a schema","Use DROP SCHEMA ... CASCADE when available and intended","Check for orphaned metastore entries when a schema 'looks' empty but the drop fails"],"tags":["hive","schema","metastore","drop"],"backgroundTag":"schema-not-empty","analyzedSha":"55bb57d202de3b926896fa966c2c4a44c779634e","analyzedAt":"2026-09-04T12:50:26.162Z","contentChangedAt":"2026-09-04T12:50:26.162Z","schemaVersion":2},"datasetVersion":"2026-09-11T21:17:09.523Z"}