{"record":{"id":"6f3ebd3027396af6","repo":"prestodb/presto","slug":"schema-not-empty-6f3ebd","errorCode":"SCHEMA_NOT_EMPTY","errorMessage":"Schema not empty: ","messagePattern":"Schema not empty: ","errorType":"error_code","errorClass":"PrestoException","httpStatus":null,"severity":"error","filePath":"presto-iceberg/src/main/java/com/facebook/presto/iceberg/IcebergHiveMetadata.java","lineNumber":359,"sourceCode":"        Database database = Database.builder()\n                .setDatabaseName(schemaName)\n                .setLocation(location)\n                .setOwnerType(USER)\n                .setOwnerName(session.getUser())\n                .build();\n\n        MetastoreContext metastoreContext = getMetastoreContext(session);\n        metastore.createDatabase(metastoreContext, database);\n    }\n\n    @Override\n    public void dropSchema(ConnectorSession session, String schemaName)\n    {\n        shouldRunInAutoCommitTransaction(\"DROP SCHEMA\");\n        // basic sanity check to provide a better error message\n        if (!listTables(session, Optional.of(schemaName)).isEmpty() ||\n                !listViews(session, Optional.of(schemaName)).isEmpty()) {\n            throw new PrestoException(SCHEMA_NOT_EMPTY, \"Schema not empty: \" + schemaName);\n        }\n        MetastoreContext metastoreContext = getMetastoreContext(session);\n        metastore.dropDatabase(metastoreContext, schemaName);\n    }\n\n    @Override\n    public void renameSchema(ConnectorSession session, String source, String target)\n    {\n        shouldRunInAutoCommitTransaction(\"RENAME SCHEMA\");\n        MetastoreContext metastoreContext = getMetastoreContext(session);\n        metastore.renameDatabase(metastoreContext, source, target);\n    }\n\n    @Override\n    public ConnectorOutputTableHandle beginCreateTable(ConnectorSession session, ConnectorTableMetadata tableMetadata, Optional<ConnectorNewTableLayout> layout)\n    {\n        SchemaTableName schemaTableName = tableMetadata.getTable();\n        String schemaName = schemaTableName.getSchemaName();","sourceCodeStart":341,"sourceCodeEnd":377,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-iceberg/src/main/java/com/facebook/presto/iceberg/IcebergHiveMetadata.java#L341-L377","documentation":"dropSchema() performs a sanity check before removing the database from the metastore: if any tables or views still exist in the schema, it refuses with SCHEMA_NOT_EMPTY so users get a clearer message than the metastore's underlying failure. It lists both Iceberg tables and views in the schema.","triggerScenarios":"DROP SCHEMA <name> (with or without CASCADE handled upstream) while the schema still contains at least one table or view visible to this connector; listTables or listViews returns non-empty.","commonSituations":"Forgetting to drop tables before DROP SCHEMA; hidden tables created by other engines that the user forgot about; a failed migration left orphaned tables in the schema.","solutions":["List and drop all tables: run SHOW TABLES IN <schema> then DROP TABLE each one","Also drop any views in the schema (SHOW VIEWS IN <schema>)","Use DROP SCHEMA ... CASCADE if the engine/session supports it to drop contents along with the schema","If tables are stale/foreign, drop them via the connector that owns them first"],"exampleFix":"// before\nDROP SCHEMA sales; -- Schema not empty: sales\n// after\nDROP TABLE sales.orders;\nDROP TABLE sales.customers;\nDROP SCHEMA sales;","handlingStrategy":"try-catch","validationCode":"-- Check emptiness before dropping:\nSELECT count(*) FROM system.metadata.tables WHERE schema_name = 'sales';\nSHOW TABLES FROM sales;\nSHOW VIEWS FROM sales;","typeGuard":null,"tryCatchPattern":"try {\n    stmt.execute(\"DROP SCHEMA sales\");\n} catch (SQLException e) {\n    if (e.getMessage() != null && e.getMessage().contains(\"Schema not empty\")) {\n        // enumerate & drop children (or use CASCADE), then retry\n    } else { throw e; }\n}","preventionTips":["Script schema teardown to drop tables/views before the schema","Prefer DROP SCHEMA ... CASCADE when supported","Audit for orphan tables created by other engines before dropping","Use IF EXISTS guards in cleanup scripts"],"tags":["iceberg","schema-not-empty","drop-schema"],"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"}