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() and putenv(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 rejected clearenv(). πŸ’” 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 of clearenv() is to adjust the value of the pointer environ(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
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms