Discard Curl Session Cookies Keep Persistent Cookies

This article explains how to configure the curl command-line tool to discard temporary session cookies while preserving persistent cookies. You will learn the specific command-line options required to ignore session-based cookies from a cookie jar during subsequent HTTP requests.

In web communications, session cookies are temporary cookies without an expiration date that expire when the session ends. Persistent cookies, on the other hand, contain a specific expiration date and remain valid across multiple sessions.

To make curl discard session cookies but keep persistent ones, you must use the -j (or --junk-session-cookies) option in combination with the cookie read (-b) and write (-c) flags.

The Command

Use the following command structure:

curl -b cookies.txt -c cookies.txt -j https://example.com

How It Works

  • -b cookies.txt (or --cookie): Tells curl to read existing cookies from the specified file (cookies.txt) and send them with the request.
  • -j (or --junk-session-cookies): Instructs curl to discard all session cookies loaded from the cookie file. Persistent cookies with a future expiration date are still loaded and sent.
  • -c cookies.txt (or --cookie-jar): Saves all remaining valid cookies (the persistent ones loaded from the file plus any new cookies received during the current request) back to the file once the operation completes.

By combining these flags, curl effectively filters out any session cookies from your cookie jar before making the request, while leaving your persistent login sessions or tracking cookies intact.