  	if [ -n "$deplist" ]; then
  		echo "===>>> Warning: Ports with dependencies on ${EXPUNGE}:"
  		for dep in $deplist; do
-@@ -2048,7 +2165,8 @@
+@@ -2048,7 +2167,8 @@
  		done
  		get_answer_yn n "\n\t===>>> Delete this dependency data"
  		case "$?" in

 
 	if [ -e "$pdb/$1/+REQUIRED_BY" ]; then
 		sort $pdb/$1/+REQUIRED_BY | cmp -s $grep_deps - || do_update=do_update
@@ -1323,12 +1398,19 @@
 	init_packages_var
 
 	echo "===>>> Checking for stale packages"
+	local ext
+	ext=tbz
+	[ -n "$use_pkgng" ] && ext=txz
 	for package in `find $PACKAGES -type f | sort`; do
-		pkg_dir=${package##*/} ; pkg_dir=${pkg_dir%\.tbz} ; echo ''
+		pkg_dir=${package##*/} ; pkg_dir=${pkg_dir%\.${ext}} ; echo ''
 
-		origin=`tar -O -zxvf $package '+CONTENTS' 2>/dev/null | grep '@comment ORIGIN:'` ||
-			fail Empty origin in $package
-		origin=${origin#@comment ORIGIN:}
+		if [ -z "$use_pkgng" ]; then
+			origin=`tar -O -zxvf $package '+CONTENTS' 2>/dev/null | grep '@comment ORIGIN:'` ||
+				fail Empty origin in $package
+			origin=${origin#@comment ORIGIN:}
+		else
+			origin=`pkg query "%o" $package` || fail Empty origin in $package
+		fi
 
 		if [ -z "$PM_INDEX" ]; then
 			if [ -d "$pd/$origin" ]; then
@@ -1344,8 +1426,21 @@
 		fi
 
 		if [ -n "$port_ver" ]; then
-			if [ "${port_ver}.tbz" = "${package##*/}" ]; then
+			if [ "${port_ver}.${ext}" = "${package##*/}" ]; then
 				echo "===>>> ${package##*/} is up to date"
+				if [ -n "$use_pkgng" ]; then
+					if pkg info -e ${pkg_dir}; then
+						echo "	===>>> $pkg_dir is not installed"
+						echo "	===>>> Path: ${package}"
+						get_answer_yn y "\n\t===>>> Delete stale package: ${package##*/}"
+						case "$?" in
+							0)	echo "	===>>> Deleting $package"
+								pm_unlink_s $package ;;
+						esac
+					fi
+					unset port_ver
+					continue
+				fi
 				if [ ! -d "${pdb}/${pkg_dir}" ]; then
 					echo "	===>>> $pkg_dir is not installed"
 					echo "	===>>> Path: ${package}"
@@ -1406,6 +1501,7 @@
 fi	# [ -n "$CLEAN_PACKAGES" ]
 
 if [ -n "$CHECK_DEPENDS" ]; then

 	[ -d "$port_dbdir" ] ||
 		fail 'PORT_DBIR is empty, or the directory $port_dbdir does not exist'
 
@@ -1610,14 +1709,19 @@
 				unset port_ver
 			fi
 		else
