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

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.