title: How to remove a cookie from CookieCollection?
body: <p>This is my code. It is get/set/remove multiple cookies at once. Set and remove (all) works fine but I can't remove just an expired cookie from <code>CookieCollection</code>.</p>
  
  <pre><code>using System;
  using System.Text;
  using System.Windows.Forms;
  using System.Net;
  using System.IO;
  using System.Web;
  using Newtonsoft.Json;
  
  namespace SetGetMultipleCookies
  {
      public partial class GetSetCookiesForm : Form
      {
          string readCookiesUrl = "http://test.dev/_test/cookies/readcookie.php";
          string setCookiesUrl = "http://test.dev/_test/cookies/setcookie.php";
          CookieContainer cookieHeader = new CookieContainer();
          CookieCollection cookie_collection = new CookieCollection();
  
          public GetSetCookiesForm()
          {
              InitializeComponent();
          }
  
          private void getCookiesButton_Click(object sender, EventArgs e)
          {
              // begins variable for page content.
              string pageSource;
              // send url request to web page.
              HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(this.readCookiesUrl);
              HttpWebResponse response = (HttpWebResponse)request.GetResponse();
              foreach (Cookie each_cookie in this.cookie_collection)
              {
                  if (!each_cookie.Expired)
                  {
                      request.Headers.Add("Cookie", each_cookie.ToString());
                  } else
                  {
                      // how to remove an expired cookie from cookie collection?
  
                  }
              }
  
              // read the page content
              using (StreamReader sr = new StreamReader(response.GetResponseStream()))
              {
                  pageSource = sr.ReadToEnd();
              }
  
              // display debug.
              resultBox.Text = pageSource + "\r\n";
              foreach (Cookie each_cookie in this.cookie_collection)
              {
                  resultBox.Text += each_cookie.ToString() + "; expires=" + each_cookie.Expires + "; path=" + each_cookie.Path + ";domain=" + each_cookie.Domain + "\r\n";
                  if (each_cookie.Expired)
                  {
                      resultBox.Text += "cookie expired.\r\n";
                  }
              }
          }
  
          private void setCookiesButton_Click(object sender, EventArgs e)
          {
              // send url request to set cookie.
              HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(this.setCookiesUrl);
              request.Method = "GET";
              request.CookieContainer = this.cookieHeader;
              request.ContentType = "application/x-www-form-urlencoded";
              HttpWebResponse response = (HttpWebResponse)request.GetResponse();
              // set cookies
              this.cookie_collection = response.Cookies;
              // debug
              resultBox.Text = "Headers ==========\r\n";
              foreach (string each_header in response.Headers)
              {
                  resultBox.Text += each_header + " = " + response.Headers[each_header] + "\r\n";
              }
              resultBox.Text += "\r\nCookies (" + response.Cookies.Count + ") ==========\r\n";
              foreach (Cookie each_cookie in response.Cookies)
              {
                  resultBox.Text += each_cookie.ToString() + "\r\n";
                  resultBox.Text += each_cookie.Name + "\r\n";
                  resultBox.Text += each_cookie.Value + "\r\n";
                  resultBox.Text += each_cookie.Expires + "\r\n";
                  resultBox.Text += each_cookie.Path + "\r\n";
                  resultBox.Text += each_cookie.Domain + "\r\n";
                  resultBox.Text += each_cookie.Secure + "\r\n";
                  resultBox.Text += each_cookie.HttpOnly + "\r\n";
                  resultBox.Text += each_cookie.Expired + "\r\n";
                  resultBox.Text += "\r\n";
              }
              // get response body.
              using (Stream responseStream = response.GetResponseStream())
              {
                  StreamReader reader = new StreamReader(responseStream, Encoding.GetEncoding("utf-8"));
                  String responseText = reader.ReadToEnd();
                  // json decode
                  //LoginResponse responsej = JsonConvert.DeserializeObject&lt;LoginResponse&gt;(responseText);
                  // display debug.
                  resultBox.Text += "Response body ==========\r\n";
                  resultBox.Text += responseText + "\r\n";
              }
          }
  
          private void removeCookiesButton_Click(object sender, EventArgs e)
          {
              this.cookieHeader = new CookieContainer();
              this.cookie_collection = new CookieCollection();
  
              resultBox.Text = "Logged out.";
          }
      }
  }
  </code></pre>
  
  <p>In the <code>getCookiesButton_Click</code> method, I just want to remove an expired cookie from <code>CookieCollection</code>. How to do that?
  How to remove an expired cookie from <code>CookieCollection</code>?</p>
  
  <p><a href="https://i.stack.imgur.com/9JCsW.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/9JCsW.png" alt="enter image description here"></a></p>
  
  <p>In the red rectangle line, that is the expired cookie first time when click on <strong>Get cookies</strong> it is showing there with expired report which is correct but second time it should be removed completely from the <code>CookieCollection</code>.</p>