How to Clear a MQ Queue from a Script or Program

Have you ever wanted to clear a MQ queue? A quick and easy way to do it is to use runmqsc’s CLEAR QLOCAL command. This method works very well as long as no application has the local queue opened for input.

Method #1:

If you need to clear a queue on Linux, Unix, Windows or IBM i (OS/400) then start runmqsc and issue the following MQSC command:

clear ql(TEST.Q.NAME)

Method #2:

On IBM i (OS/400), the user can also use the CLRMQMQ command. To issue the CLRMQMQ command, type the following on the 5250 terminal (use your queue manager and queue names):

CLRMQMQ QNAME(TEST.Q.NAME) MQMNAME(QMgrName)

Method #3:

For Unix / Linux, the following simple shell script will work to clear a queue of a local queue manager:

#!/bin/sh
#
# Clear a MQ queue (clearqueue.sh)
#
# Parameters:
#  $1=Queue manager
#  $2=Queue name
#

if [ $# -ne 2 ] ; then
#  Tell the user what they did wrong!
   echo "Usage:"
   echo "   clearqueue.sh QMgrName QueueName"
   exit;
fi

echo "clear qlocal($2)" | runmqsc $1

exit

To run the Unix/Linux script, open a shell prompt and type the following (use your queue manager and queue names):

clearqueue.sh QMgrName QueueName

Method #4:

For Windows, the following simple batch file will work to clear a queue of a local queue manager:

@echo off

REM Clear a MQ queue (clearqueue.bat)

REM Parameters:
REM  %1=Queue manager
REM  %2=Queue name

if %1. == .  goto help
if %2. == .  goto help

echo clear qlocal(%2) | runmqsc %1
goto theend

rem Tell the user what they did wrong!
:help
echo Usage:
echo    clearqueue.bat QMgrName QueueName
:theend

To run the Windows batch script, open a command prompt and type the following (use your queue manager and queue names):

clearqueue.bat QMgrName QueueName

Method #5:

You can also download and compile the ClearQ program that is listed on the following web page:
https://www.capitalware.com/mq_code_c.html

Again, this method works very well as long as no application has the queue opened for input.

To run the program on Linux, Unix, Windows, OS/400 or z/OS, open a prompt and type the following (use your queue manager and queue names):

clearq QueueName QMgrName

Method #6:

If you need to clear a queue when another application is reading from the queue (opened for input) then you can download and compile the EmptyQ program that is listed on the following web page:
https://www.capitalware.com/mq_code_java.html

To run the program on Linux, Unix, Windows, IBM i (OS/400) or z/OS, open a prompt and type the following (use your queue manager and queue names):

java EmptyQ -m QMgrName -h hostname -p port## -c ChannelName -q QueueName

Method #7:

If you need to clear a queue on z/OS then the following JCL will work (use your queue manager and queue names):

//STEP01   EXEC PGM=CSQUTIL,
//            PARM='QMGR',
//            COND=(0,LT)
//STEPLIB  DD DISP=SHR,DSN=HLQ.SCSQANLE
//         DD DISP=SHR,DSN=HLQ.SCSQAUTH
//SYSIN    DD *
COMMAND
//CSQUCMD  DD *
CLEAR QLOCAL(TEST.Q.NAME)
/*
//SYSPRINT DD SYSOUT=*

I hope this posting helps with your message management issues.

Regards,
Roger Lacroix
Capitalware Inc.

This entry was posted in IBM i (OS/400), IBM MQ, Java, Linux, macOS (Mac OS X), Open Source, Programming, Unix, Windows, z/OS.

6 Responses to How to Clear a MQ Queue from a Script or Program