Before you can drop a stream or table that’s populated by a query in KSQL, you have to terminate any queries upon which the object is dependent. Here’s a bit of jq
& xargs
magic to terminate all queries that are currently running
curl -s -X "POST" "http://localhost:8088/ksql" \
-H "Content-Type: application/vnd.ksql.v1+json; charset=utf-8" \
-d '{"ksql": "SHOW QUERIES;"}' | \
jq '.[].queries[].id' | \
xargs -Ifoo curl -X "POST" "http://localhost:8088/ksql" \
-H "Content-Type: application/vnd.ksql.v1+json; charset=utf-8" \
-d '{"ksql": "TERMINATE 'foo';"}'
To drop all streams:
curl -s -X "POST" "http://localhost:8088/ksql" \
-H "Content-Type: application/vnd.ksql.v1+json; charset=utf-8" \
-d '{"ksql": "SHOW STREAMS;"}' | \
jq '.[].streams[].name' | \
xargs -Ifoo curl -X "POST" "http://localhost:8088/ksql" \
-H "Content-Type: application/vnd.ksql.v1+json; charset=utf-8" \
-d '{"ksql": "DROP STREAM 'foo';"}'
To drop all tables:
curl -s -X "POST" "http://localhost:8088/ksql" \
-H "Content-Type: application/vnd.ksql.v1+json; charset=utf-8" \
-d '{"ksql": "SHOW TABLES;"}' | \
jq '.[].tables[].name' | \
xargs -Ifoo curl -X "POST" "http://localhost:8088/ksql" \
-H "Content-Type: application/vnd.ksql.v1+json; charset=utf-8" \
-d '{"ksql": "DROP TABLE 'foo';"}'
For more notes on the KSQL REST API see the docs and this cheatsheet.
Courtesy of Evelyn Munkel via the 💬 Confluent Community Slack group, a way to target just a particular stream:
echo Terminating the write query for the FOO stream
curl -s -X "POST" ${ksql.url} \
-H "Content-Type: application/vnd.ksql.v1+json; charset=utf-8" \
-d '{"ksql": "DESCRIBE FOO;"}' \ |
jq '.[].sourceDescription.writeQueries[].id' | \
xargs -Iq1 curl -X "POST" ${ksql.url} \
-H "Content-Type: application/vnd.ksql.v1+json; charset=utf-8" \
-d '{"ksql": "TERMINATE 'q1';"}' | jq
echo Terminating the read query for the FOO stream
curl -s -X "POST" ${ksql.url} \
-H "Content-Type: application/vnd.ksql.v1+json; charset=utf-8" \
-d '{"ksql": "DESCRIBE FOO;"}' \ |
jq '.[].sourceDescription.readQueries[].id' | \
xargs -Iq3 curl -X "POST" ${ksql.url} \
-H "Content-Type: application/vnd.ksql.v1+json; charset=utf-8" \
-d '{"ksql": "TERMINATE 'q3';"}' | jq
echo Dropping the FOO stream
curl -X "POST" ${ksql.url} \
-H "Content-Type: application/vnd.ksql.v1+json; charset=utf-8" \
-d '{"ksql": "DROP TABLE FOO;"}' | jq