How to delete rows in ClickHouse

Galaxy Glossary

How do I delete specific rows in ClickHouse?

Remove specific rows from a ClickHouse table using ALTER TABLE … DELETE WHERE.

Sign up for the latest in SQL knowledge from the Galaxy Team!
Welcome to the Galaxy, Guardian!
Oops! Something went wrong while submitting the form.

Description

Table of Contents

Why use ALTER TABLE DELETE in ClickHouse?

ClickHouse is append-only; direct DELETE is unavailable. Instead, ALTER TABLE … DELETE WHERE marks parts containing matching rows for background removal, giving near-instant logical deletes.

What is the exact DELETE syntax?

Run ALTER TABLE [db.]table DELETE WHERE <condition>. Conditions must reference columns, not aliases, and can combine filters with AND/OR.

Syntax details

db.table – optional database.
DELETE – keyword.
WHERE – mandatory filter; omit to avoid full-table delete errors.
• Works on MergeTree-family engines (Replacing, Summing, etc.).

How do I delete stale customer accounts?

Example: remove customers created before 2023-01-01.

ALTER TABLE Customers
DELETE WHERE created_at < '2023-01-01';

ClickHouse rewrites affected parts asynchronously; queries stop returning those rows immediately.

How to delete orders with no items?

Join filters are unsupported. Instead, pre-select IDs, then delete:

-- Step 1: create temp ID list
CREATE TEMPORARY TABLE empty_orders AS
SELECT id FROM Orders o
LEFT JOIN OrderItems oi ON o.id = oi.order_id
WHERE oi.order_id IS NULL;

-- Step 2: delete using IN()
ALTER TABLE Orders
DELETE WHERE id IN (SELECT id FROM empty_orders);

Best practices for large deletes

• Batch by partition or small ID ranges to avoid long merges.
• Run in off-peak hours.
• Monitor system.mutations for status and errors.
• Set mutations_sync for synchronous waits when needed.

What are common mistakes?

Missing WHERE triggers an error; ClickHouse prevents accidental full-table wipes.
Using JOINs inside DELETE is unsupported; use temp tables or sub-queries with IN.

How to check mutation progress?

SELECT database, table, is_done, latest_fail_reason
FROM system.mutations
WHERE table = 'Customers';

When should I prefer partition drops?

Need to remove entire time-based slices? ALTER TABLE … DROP PARTITION is faster because it avoids row-level rewrites.

Why How to delete rows in ClickHouse is important

How to delete rows in ClickHouse Example Usage


-- Remove products that have been out of stock for 90+ days
ALTER TABLE Products
DELETE WHERE stock = 0
  AND updated_at < now() - INTERVAL 90 DAY;

How to delete rows in ClickHouse Syntax


ALTER TABLE [db.]table
    DELETE WHERE <condition>;

-- Example in ecommerce context
ALTER TABLE Orders
    DELETE WHERE order_date < '2023-01-01'   -- drop legacy orders
              AND total_amount = 0;

Common Mistakes

Frequently Asked Questions (FAQs)

Can I DELETE with JOIN in ClickHouse?

No. Prepare a list of IDs in a temp table, then use IN() inside DELETE.

How long does a DELETE take?

Time depends on affected data size and server load. Check system.mutations for progress.

Does DELETE free disk space immediately?

Disk is reclaimed after background merges remove the old parts, typically within minutes to hours.

Want to learn about other SQL terms?

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo
Welcome to the Galaxy, Guardian!
Oops! Something went wrong while submitting the form.