Undefining/uninterning a symbol in Clojure

How do you undef / unintern a symbol in Clojure? (ns-unmap 'namespace 'symbol).

Suppose that you’re working at a REPL and you have namespaces a, b, and c, where c :uses both a and b (meaning that it imports their namespace contents into its own namespace.) You write a function foo in a, but then later you decide to move it to b. Bam, you get a namespace collision exception the next time you reload c.

The symbol foo in c’s namespace is already bound to your code in memory. Simply deleting the source code from a’s source file is not enough.

Of course, you could be brutish and simply kill your Clojure REPL process and restart it, but who wants to go through all that? We need a way to unbind the obsolete symbol in a. The solution is simple: just

(ns-unmap 'a 'foo)

and the symbol is removed from package a’s namespace. (This is called undef or unintern in other Lisp dialects.)

Edit: Since we’ve :used a from c, there will also now be a foo symbol in the c namespace, and we must also (ns-unmap 'c 'foo). Thanks Meikel!

2 Responses to Undefining/uninterning a symbol in Clojure