Removing BUSY leases from MikroTik DHCP pool

Whenever we’re designing a network, we’re trying to assign at least one continuous block of IP addresses for an automatic client assignment. Using the DHCP server, we can make network administration much easier. We can even use the DHCP server to assign an IP address for the servers in our network.

When the DHCP server spots a previously existing IP address from its DHCP leases pool, it will block that address from further assignment. This mechanism will prevent duplicated IP addresses in the same network segment.


The problem – a lack of the free IP addresses

These IP address will be marked as busy in the leases list. In most cases, the busy address is the static IP address of some host. If we have too many DHCP clients in the network, every unavailable lease may cause a problem for everyday operations.

01 - script

The best way to avoid such a situation is to periodically remove these busy leases. The easiest way to do this, is to use the scheduled script. Here is my script that I wrote for one customer:

# Delete BUSY leases from DHCP server

# version 1.0 (15.06.2017.)

# copyright (C) 2017. Srdjan Stanisic (


# the script will erase all DHCP leases in the status - BUSY


:local busyaddr [ip dhcp-server lease find status="busy"]

:local i

:foreach i in $busyaddr do={

/ip dhcp-server lease remove $i;



Analysis of the script

This script is very simple. It reads the list of the id numbers of the IP addresses with a status of “busy” and assigns them to the array busyaddr. I’m using the variable i as a counter. Ye olde habit left over from the days of the Turbo Pascal programming.

Then we will execute the foreach loop. This loop will take every element of the array busyaddr and then execute the command for deletion of that element.

I like to keep these service scripts short and clean. Omitting anything that can slowdown the router and CPU. This approach allows me to run the script more frequently without any negative impact on the overall performances of the device, even during peak hours.


Run the script on schedule

The last step is to make a schedule and to run the script periodically. The frequency of the script’s execution will depend on the number of busy leases and their impact on the network operation.

02 - scheduler

I found that the best schedule for this customer will be an once per day, just after the working hours. As you can see, I just called the name of the script as an action.

My friends, this is it, a big network problem with a simple solution and the short post on my blog. You can use this script for your own applications. If you find it useful, share your experience with us.

Stay tuned.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.