ClearCookie

Expire a client cookie (or all cookies if left empty)

  1. func (c *Ctx) ClearCookie(key ...string)
  1. app.Get("/", func(c *fiber.Ctx) error {
  2. // Clears all cookies:
  3. c.ClearCookie()
  4. // Expire specific cookie by name:
  5. c.ClearCookie("user")
  6. // Expire multiple cookies by names:
  7. c.ClearCookie("token", "session", "track_id", "version")
  8. // ...
  9. })

Web browsers and other compliant clients will only clear the cookie if the given options are identical to those when creating the cookie, excluding expires and maxAge. ClearCookie will not set these values for you - a technique similar to the one shown below should be used to ensure your cookie is deleted.

  1. app.Get("/set", func(c *fiber.Ctx) error {
  2. c.Cookie(&fiber.Cookie{
  3. Name: "token",
  4. Value: "randomvalue",
  5. Expires: time.Now().Add(24 * time.Hour),
  6. HTTPOnly: true,
  7. SameSite: "lax",
  8. })
  9. // ...
  10. })
  11. app.Get("/delete", func(c *fiber.Ctx) error {
  12. c.Cookie(&fiber.Cookie{
  13. Name: "token",
  14. // Set expiry date to the past
  15. Expires: time.Now().Add(-(time.Hour * 2)),
  16. HTTPOnly: true,
  17. SameSite: "lax",
  18. })
  19. // ...
  20. })