Because deleting rules in UFW can be a little bit annoying this little trick will make your life much easier. Instead of using this syntax:

ufw delete allow 80/tcp

use this syntax:

ufw delete ####

where #### is the number of your firewall rule.

The numbers of your firewall rules can listed using:

ufw status numbered

A possible output could be:

Status: active

     To                         Action      From
     --                         ------      ----
[ 1] 80/tcp                     ALLOW IN    Anywhere
[ 2] 443/tcp                    ALLOW IN    Anywhere

12 comments so far

Add Your Comment
  1. Dean Kamali

    Thank you that was helpful

    Just wondering if there is a way to delete multiple ports at the same time

  2. tersmitten

    What do you mean with multiple ports, multiple rules?

  3. zhouxin

    Very helpful!

  4. cmore


  5. zb


  6. Mike

    So helpful. Thank-you! I made a mistake entering a rule and could not figure out the syntax to remove the messed up rule. Using the number saved my day. Thanks again!

  7. Duro

    Thanks, saved my day

  8. Onni

    Very useful! Ansible has nice methods to add ufw rules but not to delete them 🙁

  9. baser

    Thank you. I need this article.

  10. Bas Matthee

    Care for the following. When you remove a rule, having number 1, be aware that the numbering will change for the rules that are still active.

    So if I have for example:

         To                         Action      From
         --                         ------      ----
    [ 1] 22                         ALLOW IN    Anywhere                  
    [ 2] 443                        ALLOW IN    Anywhere                  
    [ 3] 80 (v6)                    DENY IN     Anywhere (v6)             
    [ 4] 8080 (v6)                  ALLOW IN    Anywhere (v6)             
    [ 5] 8443 (v6)                  ALLOW IN    Anywhere (v6)   

    If I wish to remove rule #3 and #4, I should execute `sudo ufw delete 3` followed by the same command, instead of `sudo ufw delete 4`.

    PS. I’m sorry if the code-blocks look messed up. I didn’t take time to figure out the syntax on this website.

  11. Matthias

    Helpful, Thank you!

  12. Max

    Thanks, it was very helpful.