Delete UFW Rule — Ubuntu Firewall Tutorial
In this article, we’ll see how to delete firewall rules in the UFW firewall on Ubuntu. You’ll learn how to remove rules using rule numbers, how to delete specific rules by command, and how to reset the firewall entirely.
Delete UFW Rule by Number
The command to delete a firewall rule using its number is:
ufw delete [rule number]
To find the rule numbers, use the following command:
ufw status numbered
For example, if you want to delete rule number 2, the command would be:
ufw delete 2

Important:
After deleting a rule, the rules beneath it will move up by one position. So if you try to delete another rule based on the old table, you might delete the wrong one.
Tip:
You can either recheck the firewall rules after each delete or delete rules starting from the bottom up. For example, if you want to delete rule #1 and rule #3, first delete rule #3, then delete rule #1.
Delete UFW Rule by Command
Another way to delete firewall rules is by using the actual rule itself.
For example, if you had added a rule like:
ufw allow 53
You can delete it using:
ufw delete allow 53
This method also works and is helpful when you know the exact rule.
Reset All UFW Rules
If you want to delete all rules and reset the UFW firewall to its default state, use:
ufw reset
This will:
- Delete all firewall rules
- Turn off the firewall
Re-enable UFW After Reset
If you're connected via SSH, make sure to allow port 22
before enabling the firewall again. This will prevent you from getting locked out.
ufw allow 22/tcp
Then enable the firewall:
ufw enable
By following these steps, you can safely delete rules from your UFW firewall on Ubuntu. Just remember to check the rule numbers before deleting and always allow SSH access if you're working remotely.
What’s Next?
In the next tutorial, we will learn how to open ports using UFW on Ubuntu.