\global variant of \csname…\endcsname

Instead of making the \csname assignment global, you can make it even more local:

\documentclass{article}
\tracingrestores=1

\def\foo{\bar\foobar}
\def\bar{\gdef\testA{blabb}}
\def\foobar{\begingroup\expandafter\endgroup\expandafter\gdef\csname testB\endcsname{blubb}}

\begin{document}
{\foo}

\testA
\testB

\end{document}

Now the \expandafter is executed in a group, so the \csname defined \testB to \relax in a group which ends before \gdef starts. So \testB is undefined when the global definition occurs which should avoid the retaining entry.


I don't know squat about (La)TeX stacks (other than stackengine, LOL), but if the goal is to avoid having the \csname inside a \def...

Expand the \csname before executing the outer \def.

\documentclass{article}
\tracingrestores=1

\def\foo{\bar\foobar}
\def\bar{\gdef\testA{blabb}}
\expandafter\def\expandafter\foobar\expandafter{\expandafter\gdef\csname testB\endcsname{blubb}}

\begin{document}
{\foo}

\testA
\testB

\end{document}