clearenv(3)
β¨ clearenv(3) β the "nuke the whole environment" page from the Library Functions Manual β¨
okay bestie, gather round, because today we're learning about a function that just... deletes everything. no cap. it's giving spring cleaning but for your process. π
πΈ NAME πΈ
clearenv β clears the environment, periodt. gone. all of it. π§Ή
π LIBRARY π
Standard C library (libc, link it with -lc) π«Ά
β¨ SYNOPSIS β¨
#include <stdlib.h>
int clearenv(void);
π Feature Test Macro Requirements for glibc π
(if you wanna know how feature test macros work, go peek at feature_test_macros(7))
clearenv():
/* glibc >= 2.19: */ _DEFAULT_SOURCE
|| /* glibc <= 2.19: */ _SVID_SOURCE || _BSD_SOURCE
yes the macro requirements literally change depending on your glibc version, iconic behaviour honestly π
π¦ DESCRIPTION π¦
So what does clearenv() actually do? Bestie. It clears the environment of all name-value pairs and then sets the value of the external variable environ to NULL. like, the whole thing. wiped. π
After this call, new variables can be added to the environment using putenv(3) and setenv(3). so you're not stranded, you can rebuild from scratch β main character energy, rebranding arc, whatever you wanna call it. π
β RETURN VALUE β
clearenv() returns zero on success, and a nonzero value on failure. classic. zero = slay, nonzero = not slay. π
π ATTRIBUTES π
For an explanation of the terms used in this section, see attributes(7). β¨
- Interface:
clearenv() - Attribute: Thread safety
- Value:
MT-Unsafe const:env
so basically, thread safety? MT-Unsafe const:env. noted. tread carefully, queen. π
π STANDARDS π
putenv()β POSIX.1-2008.clearenv()β None.
yeah. clearenv() isn't in any standard. it's the ungovernable one. no rules, just vibes π¦
π°οΈ HISTORY π°οΈ
putenv()β glibc 2.0. POSIX.1-2001.clearenv()β glibc 2.0.- Various UNIX variants (DG/UX, HP-UX, QNX, ...) have it too. it's not just a glibc thing, it's a whole situation.
- POSIX.9 (bindings for FORTRAN77). yep, FORTRAN77. we love a crossover episode. π
- POSIX.1-1996 did not accept
clearenv()andputenv(3), but then it changed its mind and scheduled these functions for some later issue of this standard (see Β§B.4.6.1). - However, POSIX.1-2001 adds only
putenv(3), and rejectedclearenv(). π rejected. not even a maybe. ouch.
π NOTES π
On systems where clearenv() is unavailable, this assignment
environ = NULL;
will probably do. probably. do with that information what you will. π
The clearenv() function may be useful in security-conscious applications that want to precisely control the environment that is passed to programs executed using exec(3). The application would do this by first clearing the environment and then adding select environment variables. like a bouncer for your env vars, only the chosen ones get in. π
important, bestie: note that the main effect ofclearenv()is to adjust the value of the pointerenviron(7); this function does not erase the contents of the buffers containing the environment definitions. the pointer gets ghosted, the buffers are still literally sitting there. π«₯
And the drama continues β the DG/UX and Tru64 man pages write: If environ has been modified by anything other than the putenv(3), getenv(3), or clearenv() functions, then clearenv() will return an error and the process environment will remain unchanged. so don't be chaotic with it or nothing happens. π
π₯ SEE ALSO π₯
and that's the whole page diva π this was the Linux man-pages 6.20 version, dated 2026-02-08 β go forth, clear that environment, and remember: it's not deleted, it's just unlinked. β¨π¦π
π¦ where this page comes from
- arch rolling: man-pages 6.20-1 Β· BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages 6.20
- π the page's license: Linux-man-pages-copyleft
- Copyright 2001, John Levon <moz@compsoc.man.ac.uk>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms