deleting all records for all tables in memory in Kdb+

Viewed 337

I would like to delete all records for all tables in memory but still keep the schemas.

for example:

a:([]a:1 2;b:2 4);
b:([]c:2 3;d:3 5);

I wrote a function:

{[t] t::select from t where i = -1} each tables[]

this didnt work, so i tried

{[t] ![`t;enlist(=;`i;-1);0b;()]} each tables[]

didnt work either. Any idea or better ways?

2 Answers

If you pass a global table name as a symbol, it removes all the rows, leaving an empty table

q)delete from `a
`a

q)a
a b
---

q)meta a
c| t f a
-| -----
a| j    
b| j    

To do it for all global tables in root name space

{delete from x} each tables[]

Your second attempt using function was close. You can achieve it via the following (functional form of the above):

![;();0b;`symbol$()] each tables[]
  • The first argument should be the symbol of the table for the same reason I mentioned before
  • The second argument should be an empty list as we want to delete all records (we do not want to delete where i=-1, as that would delete nothing)
  • The final argument (list of columns to delete) should be an empty symbol list instead of an empty general list.

Mark's solution is best for doing what you want rather than functional form. Just adding to your question on t failing as putting kdb code in comments is awkward.

Your functional form fails not because of the t but because your last argument is not a symbol list `$(). Also you would want to delete where i is > -1, not =

q){[t] ![t;enlist(>;`i;-1);0b;`$()]} each `d`t`q
`d`t`q

q)d
date sym time bid1 bsize1 bid2 bsize2 bid3 bsize3 ask1 asize1 ask2 asize2 ask..
-----------------------------------------------------------------------------..
q)t
date sym time src price size
----------------------------
q)q
date sym time src bid ask bsize asize
-------------------------------------

Related