Skip to main content

Overview

DROP commands permanently remove database objects from Materialize. Use these commands carefully, as dropping objects also affects all dependent objects.
DROP operations are permanent and cannot be undone. Always verify dependencies before dropping objects.

Available DROP Commands

DROP CLUSTER

Remove a cluster and all its replicas

DROP INDEX

Remove an index

DROP SOURCE

Remove a source and its subsources

DROP MATERIALIZED VIEW

Remove a materialized view

DROP VIEW

Remove a regular view

DROP SINK

Remove a sink

DROP TABLE

Remove a table

DROP CONNECTION

Remove a connection

DROP SECRET

Remove a secret

DROP DATABASE

Remove a database and all its objects

DROP SCHEMA

Remove a schema and all its objects

DROP ROLE

Remove a role

Syntax Patterns

Basic DROP

-- Drop a materialized view
DROP MATERIALIZED VIEW my_view;

-- Drop a source
DROP SOURCE my_source;

-- Drop a cluster
DROP CLUSTER my_cluster;

CASCADE vs RESTRICT

-- Drop with CASCADE (removes dependent objects)
DROP MATERIALIZED VIEW my_view CASCADE;

-- Drop with RESTRICT (fails if dependencies exist) - default behavior
DROP MATERIALIZED VIEW my_view RESTRICT;
CASCADE will drop all dependent objects without additional confirmation. Use with extreme caution in production.

IF EXISTS

-- Safely drop if exists (no error if doesn't exist)
DROP MATERIALIZED VIEW IF EXISTS my_view;

DROP SOURCE IF EXISTS my_source CASCADE;

Checking Dependencies

Before dropping objects, check for dependencies:
-- Find objects that depend on a view
SELECT
    dependent.name AS dependent_object,
    dependent.type AS object_type
FROM mz_catalog.mz_objects dependent
JOIN mz_internal.mz_object_dependencies dep ON dependent.id = dep.object_id
JOIN mz_catalog.mz_objects target ON dep.referenced_object_id = target.id
WHERE target.name = 'my_view';

-- Find all dependencies recursively
WITH RECURSIVE deps AS (
    SELECT id, name
    FROM mz_catalog.mz_objects
    WHERE name = 'my_view'
    UNION
    SELECT o.id, o.name
    FROM mz_catalog.mz_objects o
    JOIN mz_internal.mz_object_dependencies d ON o.id = d.object_id
    JOIN deps ON d.referenced_object_id = deps.id
)
SELECT * FROM deps;

Common Use Cases

Clean Up Test Objects

-- Drop test views
DROP MATERIALIZED VIEW IF EXISTS test_mv CASCADE;
DROP VIEW IF EXISTS test_view;

-- Drop test sources
DROP SOURCE IF EXISTS test_source CASCADE;

Remove Unused Clusters

-- First, check what's running on the cluster
SELECT name, type
FROM mz_catalog.mz_objects
WHERE cluster_id = (
    SELECT id FROM mz_catalog.mz_clusters WHERE name = 'old_cluster'
);

-- Drop the cluster (will fail if objects still use it)
DROP CLUSTER old_cluster;

Drop with Dependency Chain

-- Drop entire hierarchy
DROP MATERIALIZED VIEW top_level_view CASCADE;
-- This will also drop all views, indexes, and sinks that depend on it

Best Practices

Check First

Always query dependencies before dropping objects

Use IF EXISTS

Prevent errors in scripts with IF EXISTS clause

Avoid CASCADE in Prod

Be extremely careful with CASCADE in production environments

Document Changes

Keep track of dropped objects for troubleshooting

Cleanup Patterns

Drop All Objects in a Schema

-- List objects in schema first
SELECT name, type FROM mz_catalog.mz_objects
WHERE schema_id = (SELECT id FROM mz_catalog.mz_schemas WHERE name = 'old_schema');

-- Drop schema and all contents
DROP SCHEMA old_schema CASCADE;

Drop All Test Objects

-- Drop all objects with 'test_' prefix
DROP MATERIALIZED VIEW IF EXISTS test_orders CASCADE;
DROP SOURCE IF EXISTS test_kafka_source CASCADE;
DROP CLUSTER IF EXISTS test_cluster CASCADE;

CREATE Commands

Create new database objects

ALTER Commands

Modify existing database objects

SHOW Commands

Inspect database objects

System Catalog

Query object metadata

Build docs developers (and LLMs) love