# Remove Nat Sessions on a specific event

**URL:** <https://forum.mikrotik.com/t/remove-nat-sessions-on-a-specific-event/149214>\
**Category:** Scripting\
**Created:** [May 20, 2021, 2:03pm UTC](https://forum.mikrotik.com/t/remove-nat-sessions-on-a-specific-event/149214 "2021-05-20T14:03:06Z")\
**Posts on this page:** 3\
**Page:** 2

<div class="post-metadata">

**Author:** ![AdminAdmin123](https://forum.mikrotik.com/user_avatar/forum.mikrotik.com/adminadmin123/32/2804_2.png) [@AdminAdmin123](https://forum.mikrotik.com/u/AdminAdmin123)\
**Post date:** [January 19, 2022, 8:58am UTC](https://forum.mikrotik.com/t/remove-nat-sessions-on-a-specific-event/149214/21 "2022-01-19T08:58:17Z")

</div>

> [@](#):
>
> create a netwatch for that IP and put on both on-down and on-up this:
> 
> ```plaintext
> /ip fire conn
> :foreach idc in=[find where (timeout > 60) and (reply-dst-address ~ "10.254.251.254")] do={
> remove [find where .id=$idc]
> }
> 
> ```
> 
> please do not alter the timeout
> 
> [Dual WAN Failover Script Ping Command](http://forum.mikrotik.com/t/dual-wan-failover-script-ping-command/150516/1)
> 
> > I do not write the things without reason.  
> > If just one connection on connection tracking is already closed for timeout (or other reasons) during the execution of the clean,  
> > the script stop with error because when try the connection is already closed, and do not finish his works.

Ciao Rextended,  
Unfortunatly (probably for my mistakes or fault) this is not working for me. By the way I've found a workaround to make it successfull.  
With your high knowledge on Mikrotik scripts, I wanna ask you if is possible to create a script (that I'll implement with /system scheduler) that check if the default gateway have changed, and if yes launch a command /ip firewall connection remove blablabla that I already know is working cause I've tested it, but at this very moment I run it manually.  
If I am not been clear let me know  
Thank you in advance

---

<div class="post-metadata">

**Author:** ![AdminAdmin123](https://forum.mikrotik.com/user_avatar/forum.mikrotik.com/adminadmin123/32/2804_2.png) [@AdminAdmin123](https://forum.mikrotik.com/u/AdminAdmin123)\
**Post date:** [February 14, 2022, 2:14pm UTC](https://forum.mikrotik.com/t/remove-nat-sessions-on-a-specific-event/149214/22 "2022-02-14T14:14:47Z")

</div>

> [@](#):
>
> > [@](#):
> >
> > create a netwatch for that IP and put on both on-down and on-up this:
> > 
> > ```plaintext
> > /ip fire conn
> > :foreach idc in=[find where (timeout > 60) and (reply-dst-address ~ "10.254.251.254")] do={
> > remove [find where .id=$idc]
> > }
> > 
> > ```
> > 
> > please do not alter the timeout
> > 
> > [Dual WAN Failover Script Ping Command](http://forum.mikrotik.com/t/dual-wan-failover-script-ping-command/150516/1)
> 
> Ciao Rextended,  
> Unfortunatly (probably for my mistakes or fault) this is not working for me. By the way I've found a workaround to make it successfull.  
> With your high knowledge on Mikrotik scripts, I wanna ask you if is possible to create a script (that I'll implement with /system scheduler) that check if the default gateway have changed, and if yes launch a command /ip firewall connection remove blablabla that I already know is working cause I've tested it, but at this very moment I run it manually.  
> If I am not been clear let me know  
> Thank you in advance

Sorry for the bump, does anyone know or have any suggestion how to implement that?

---

<div class="post-metadata">

**Author:** ![rextended](https://forum.mikrotik.com/user_avatar/forum.mikrotik.com/rextended/32/67729_2.png) [@rextended](https://forum.mikrotik.com/u/rextended)\
**Post date:** [February 15, 2022, 8:19am UTC](https://forum.mikrotik.com/t/remove-nat-sessions-on-a-specific-event/149214/23 "2022-02-15T08:19:09Z")

</div>

How to obtain actual Gateway ID, compare it on previous stored ID value on memory, and if is different, remove all old NAT sessions

For debug  
:global previousgw  
:if ([:typeof $previousgw] != “id”) do={ :set $previousgw \*0 }  
:global defaultgw ([/ip route get [find where dst-address=0.0.0.0/0 and active=yes and !routing-mark]]-\>“.id”)  
:if ($previousgw != $defaultgw) do={  
:log warning “Previous GW ID $previousgw different to current GW ID $defaultgw”  
:set $previousgw $defaultgw  
} else={  
:log info “The Gateway ID $defaultgw still the same”  
}

Working example without debug frills  
:global previousgw  
:if ([:typeof $previousgw] != “id”) do={ :set $previousgw \*0 }  
:global defaultgw ([/ip route get [find where dst-address=0.0.0.0/0 and active=yes and !routing-mark]]-\>“.id”)  
:if ($previousgw != $defaultgw) do={  
/ip fire conn  
:foreach idc in=[find where (timeout \> 60)] do={  
remove [find where .id=$idc]  
}  
:set $previousgw $defaultgw  
}

[Previous page](https://forum.mikrotik.com/t/remove-nat-sessions-on-a-specific-event/149214.md?page=1)
