quant
quant

Reputation: 23052

How do I delete tables matching a search criterion?

I know I can delete tables using

delete some_table_name from `.

But suppose I have a large number of tables, and want to delete all tables that, say, begin with prefix_ and end with _suffix.

How can I do this?

Upvotes: 0

Views: 368

Answers (2)

Alexander Belopolsky
Alexander Belopolsky

Reputation: 2268

If you require this functionality often, I suggest that you define a drop function as follows:

q)drop:![`.;();0b;](),

This function will take one or more table names as symbols and delete them. Combined with a selector function it can be used to delete by pattern

q)drop{x where x like"prefix_*_suffix"}tables[]

You can also define a drop_matching function

q)drop_matching:drop{a where(a:tables[])like x}@

that will do the job in one swoop:

q)drop_matching"prefix_*_suffix"

Upvotes: 1

MdSalih
MdSalih

Reputation: 1996

You can use functional form of delete (see here):

/ create some tables 
q)`a_one`a_two`b_one`b_two set\:([] x:til 10)
    `a_one`a_two`b_one`b_two
q)tables[]
    `s#`a_one`a_two`b_one`b_two

/ find table names matching "a_*" and delete them from root namespace
q)![`.;();0b;{x where x like "a_*"} tables[]]
    `.
q)tables[]
    `s#`b_one`b_two

Upvotes: 1

Related Questions