Skip to content

Instantly share code, notes, and snippets.

@lildude
Created March 20, 2017 12:11
Show Gist options
  • Save lildude/4b76b96cb1c0d0b669fc3eeb6ea1187e to your computer and use it in GitHub Desktop.
Save lildude/4b76b96cb1c0d0b669fc3eeb6ea1187e to your computer and use it in GitHub Desktop.
Flush and delete all nftables rules, chains and tables
#!/bin/bash
#
# Name: nft-flush
# Auth: Gavin Lloyd <gavinhungry@gmail.com>
# Date: 06 Mar 2014
# Desc: Flush and delete all nftables rules, chains and tables
#
NFT=/usr/bin/nft
FAMILIES="ip ip6 arp bridge"
for FAMILY in $FAMILIES; do
TABLES=$($NFT list tables $FAMILY | grep "^table\s" | cut -d' ' -f2)
for TABLE in $TABLES; do
CHAINS=$($NFT list table $FAMILY $TABLE | grep "^\schain\s" | cut -d' ' -f2)
for CHAIN in $CHAINS; do
echo "Flushing chain: $FAMILY->$TABLE->$CHAIN"
$NFT flush chain $FAMILY $TABLE $CHAIN
$NFT delete chain $FAMILY $TABLE $CHAIN
done
echo "Flushing table: $FAMILY->$TABLE"
$NFT flush table $FAMILY $TABLE
$NFT delete table $FAMILY $TABLE
done
done
@ohhai
Copy link

ohhai commented Nov 13, 2021

For me it requires TABLES=... -f3 (not -f2)
Using Fedora 35

Also: nft flush ruleset

@MrJake222
Copy link

MrJake222 commented Feb 5, 2023

Should be -f3 as mentioned by @ohhai.

Edit: Better solution than listing families would be:

#!/bin/bash

nft list tables |
while read table; do
	nft delete $table
done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment