Page.delete
Page.delete() is used to delete a page. This requires the "delete" user right, typically only given to site administrators. The page is hidden but remains visible to administrators. Typical usage:
page = site.Pages['User:Apple/Sandbox']
page.delete(reason='Copyright violation')
Associated API: API:Delete
-
(optional) reason: Reason for deletion.
It is recommended to always include a clear deletion reason. (Default: empty string)
- The MediaWiki API allows you to omit the reason and generates a default reason. This functionality is not available through mwclient.
-
(optional) watch: If set to
True
, also add this page to the logged-in user's watchlist. (Default:False
) -
(optional) unwatch: If set to
True
, and this page is on the logged-in user's watchlist, remove it from the watchlist. (Default:False
) -
(optional) oldimage: Allows deleting an old revision of an file
(not the entire file description page) by specifying an
oldimage
name.-
oldimage
names are obtained from thearchivename
property of pages returned by [Image.imagehistory]. See Examples below. - If the page being deleted is not a file description page, this option is ignored.
-
The page is deleted on the site and an entry is added to the deletion log. The method returns a dict with the following keys:
- 'title': the canonical title of the deleted page
- 'reason': the reason for deletion
- Because mwclient does not currrently support default reasons, this should always match the supplied reason.
- InsufficientPermission: The currently logged-in user does not have permission to delete the page.
-
APIError: Depending on the error's
code
property:- 'permissiondenied': The currently logged-in user does not have permission to delete the page.
May occur instead of
InsufficientPermission
in the case of a race condition where the user loses the delete right during thedelete()
call. - 'cantdelete': The page could not be deleted, usually because someone else already deleted it.
- 'unknown': Depends on error's "info" property. If set to "Unknown error: ``invalidoldimage''", indicates an invalid oldimage argument was passed.
- 'permissiondenied': The currently logged-in user does not have permission to delete the page.
May occur instead of
- APIDisabledError: The site has disabled access through the MediaWiki API.
- MaximumRetriesExceeded: API call to perform the delete failed and was retried until all retries were exhausted.
Deleting a page normally, adding the page to the watchlist:
page = site.Pages['User:Apple/Sandbox']
page.delete(reason='Copyright violation', watch=True)
Deletes all file revisions of the file "Test.jpg" except the most recent one:
for page in site.Pages['File:Test.jpg'].imagehistory():
if page.has_key('archivename'):
image.delete(reason='Testing', oldimage=page['archivename'])
Because it is always possible that another user may delete the file before mwclient, robust scripts should always catch APIError:
try:
page = site.Pages['User:Apple/Sandbox']
page.delete(reason='Copyright violation', watch=True)
except APIError, e:
if e.code != 'cantdelete': # Okay if already deleted
raise
This page was originally imported from the old mwclient wiki at SourceForge. The imported version was dated from 16:21, 21 February 2012, and its only editor was Derrickcoetzee (@dcoetzee).
- Querying Wikipedia with mwclient
- Replacing a string for every page in a category
- Creating a page listing all pages in a category
Note: the red links below are pages yet to be created. Feel free to add them!