Skip to main content
Ben Nadel at InVision In Real Life (IRL) 2018 (Hollywood, CA) with: Brad Brewer
Ben Nadel at InVision In Real Life (IRL) 2018 (Hollywood, CA) with: Brad Brewer

Destroying ColdFusion Variables Using JavaCast()

By on

Not really that interesting, but you can destroy variables by setting them equal to the result of a JavaCast() to "null". I have beeing some testing with null values in ColdFusion lately and I did this for a goof:

<!--- Create the objFoo variable. --->
<cfset objFoo = StructNew() />
<cfset objFoo.Bar = "Well Played!" />

<!--- Destroy struct via JavaCast(). --->
<cfset objFoo = JavaCast( "null", 0 ) />

<!--- Dump out variable. --->
<cfdump var="#objFoo#" />

This throws the error:

Variable OBJFOO is undefined.

Now, I am not recommending this, nor should this ever be a replacement for the ColdFusion function StructDelete(). In fact, if you try to use this to destroy a structure element, it doesn't "really" work. Let's rework the code to destroy the structure key, not the structure:

<!--- Create the objFoo variable. --->
<cfset objFoo = StructNew() />
<cfset objFoo.Bar = "Well Played!" />

<!--- Destroy key via JavaCast(). --->
<cfset objFoo.Bar = JavaCast( "null", 0 ) />

<!--- Dump out variable. --->
<cfdump var="#objFoo#" />

This time, when you dump out the structure, "Bar" is still a key in the structure:

undefined struct element

However, its value is "[undefined struct element]" and any attempt to reference it directly will throw the error:

Element BAR is undefined in OBJFOO.

Interesting stuff.

Want to use code from this post? Check out the license.

Reader Comments


LOL "not really interesting"? This is extremely interesting if you use isdefined() to test for the existence of a single session variable and you want to destroy that one var without clearing the whole session scope!


This helped hugely. @cfjedimaster pointed me in the Javacast direction, then i found this from @bennadel.

I have a QoQ running after drawing out the data from an XML file.

My empty variables of "" were not working and throwing a Null Pointer error with no other information and hard to debug.

This is how i fixed it:

<cfset temp = QuerySetCell(myquery, "listing_field1", JavaCast( "null", 0 ), #i#)>




You can always use structDelete() to clear variables as well (which might be a little more self-documenting).


Very nice! Query of Queries is awesome; but, they can really throw you through a loop when dealing with type-casting. I've learned to always always always use javaCast() when manually setting query values.

You might want to check out this post:

It specifically talks about maintaining proper query cell values when deserializing from XML (WDDX).


Am using coldfusion 5 and the first piece of code did not throw error - "Variable OBJFOO is undefined.".
What am looking for is a solution to memory leak issue and so am wanting that all the variables should be killed at the end of the code. Any suggestions ?

I believe in love. I believe in compassion. I believe in human rights. I believe that we can afford to give more of these gifts to the world around us because it costs us nothing to be decent and kind and understanding. And, I want you to know that when you land on this site, you are accepted for who you are, no matter how you identify, what truths you live, or whatever kind of goofy shit makes you feel alive! Rock on with your bad self!
Ben Nadel